Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I check if all bools in a List of instances of classes is true?

So I have a list of instances of classes like

Imagine you have Slime.cs and you have multiple slimes in a list: List slime = new List();

And each slime class has a bool hasHit; and you wanna check if every slime in the List has hasHit to be true. How would you do that? I understand there’s enumerable All but wouldn’t that only work for a list/array of bools? I don’t know how to do that for a List of instances of a class

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I tried to use Enumerable All, I tried using foreach and for loops too

>Solution :

Try this:

List<Slime> slimes = new List<Slime>();
bool allShasHit  = slimes.All(slime => slime.hasHit);

See Microsoft All Documentation

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading