How to use TrueForAll

13,494

Solution 1

Use All:

bool alltrue = listOfBools.All(b => b);

It will return false one the first false.

However, since you are actually using a List<bool> you can also use List.TrueForAll in the similar way:

bool alltrue = listOfBools.TrueForAll(b => b);

But since that is limited to a list i would prefer Enumerable.All.

Solution 2

One way is: You can use All..

var result = list.All(x => x);

If all are true, result will be true.

Solution 3

Probably it's confusing because it's too easy if you array already contains booleans:

List<bool> booleans;

booleans.TrueForAll(x => x);

or

booleans.All(x => x);
Share:
13,494

Related videos on Youtube

Sturm
Author by

Sturm

Updated on July 27, 2022

Comments

  • Sturm
    Sturm over 1 year

    I have a list of bools and I want to check if every one is set to true. I can run a loop and check it that way but I want to try to do it with TrueForAll method of a list. I need a predicate for that but I couldn't find a clear example for such a simple task as this.

Related