Check severeal boolean returns in same time

asp.net, boolean, boolean-logic, c#, logic

Solution

use `&&` (logical and) not `&` (binary operator)

like:

`p1() && p2();`

it will return true only if all `p*()` are true (same as `&`), but note that if first `p*()` will return false rest of expression won't be evaluated. In case of `&` whole expression will be evaluated

var functions = new List<Func<bool>>();
functions.Add(p1);
functions.Add(p2);
functions.Add(p3);
functions.Add(p4);
functions.Add(p5);
functions.Add(p6);
functions.Add(p7);
return functions.Take(idcount).All(x=>x());

try above it looks cleaner than switch statement and should examine if all first `idcount` entries are true same as switch case with `&&`

Problem

I have several bool elements and I am checking it if returns me false. ``` bool i = false; switch (idcount) { case 1: i = p1(); break; case 2: i = p1() & p2(); break; case 3: i = p1() & p2() & p3(); break; case 4: i = p1() & p2() & p3() & p4(); break; case 5: i = p1() & p2() & p3() & p4() & p5(); break; case 6: i = p1() & p2() & p3() & p4() & p5() & p6(); break; case 7: i = p1() & p2() & p3() & p4() & p5() & p6() & p7(); break; } return i; ``` I want if one of p*() returns false in any case i returns false. Is it right way or two false returns true? I want all p*() return true i returns true..

Original source