tags:

views:

349

answers:

2

I have an array of booleans which gets filled by a loop. The method that owns the array needs to return a single boolean. So can I do this:

bool[] Booleans = new bool[4];

// do work - fill array

return (Booleans[0] && Booleans[1] && Booleans[2] && Booleans[3]);

So if I have: T,T,F,T will I get F back since there is one in the array or will it send back something else or just crash all together?

+13  A: 

A single false will result in false being returned with boolean AND logic.

You can also rewrite as:

return Booleans.All(b => b);

For the sake of completeness, or if LINQ is not an option, you can achieve the same via a loop:

var list = new List<bool> { true, false, true };

bool result = true;
foreach (var item in list)
{
    result &= item;
    if (!item)
        break;
}

Console.WriteLine(result);

For small samples what you have is fine, but as the number of items grow either of the above approaches will make the code a lot friendlier.

Ahmad Mageed
I think `All` is a bit overkill here.
ChaosPandion
@ChaosPandion, why? Seems clear and concise to me.
Kirk Woll
I haven't used `All` before. Can you explain what it does exactly? And how would it differ (be better than?) from `Any` in this case?
Alex
All requires that each item be true, while Any requires that one item be true - logical AND of items VS logical OR. In this case, you want All, since all elements in the array should be true for true to be returned.
Remi Despres-Smyth
@Alex `All` is used to check that all the items meet a given criteria. Since you're dealing with booleans you would want to use `All` and check that they're all `true` as my snippet shows. `Any` is used to check that at least one item (i.e., any item) meets a given criteria. You don't want to use `Any` in this case since it wouldn't take all the elements into consideration but only return true as soon as any item meets the criteria.
Ahmad Mageed
`All` is the more restrictive brother of `Any`. It simply verifies that all elements of a sequence match a given criteria. You could use either in this case, though with opposite tests. Use `All` as demonstrated to verify all values are true, or `Any` to verify if at least one is false (and return false if so).
Anthony Pegram
@Remi, @Ahmad and @Anthony, okay now `All` makes sense to me. Thanks for the help guys!
Alex
Can't you just use !Booleans.Contains(false) then?
Nyerguds
@Nyerguds you could but it doesn't seem as clear with the `NOT` logic attached. `All` also makes it easier to check certain properties when not dealing with a sequence of booleans directly.
Ahmad Mageed
A: 

If you must process the array and return only a single boolean, then the best approach would be to just set a value to true, then loop through until the value is false, or you reach the end of the loop.

This way you won't have to process past the first false value, since that makes the entire result false.

How you loop through depends on which version of .NET you are using.

James Black