score:14

Accepted answer

You can use (renamed the collection for reasons of readability):

bool anyFalse = myBools.Any(b => !b);

or

bool anyFalse = !myBools.All(b => b);

both are efficient since they break on the first false.

If there are complex objects in the collection(as it seerms to be) use:

bool anyFalse = MyList.Any(x => !x.MyBool);

or

bool anyFalse = !MyList.All(x => x.MyBool);

score:5

Yes, it will work because Boolean is IComparable<Boolean> but it's awkward and thus it's harder to understand the intent of your code.

Additionally, your code will have to go through the whole sequence just to find out if there's a false, and it will throw if the sequence is empty.

Use All instead:

MyList.All(item => item.MyBool)

This will return false if there's any false value in the sequence. If the sequence is empty, All will return true.


Related Articles