Reputation: 133
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..
Upvotes: 9
Views: 261
Reputation: 23087
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 &&
Upvotes: 7
Reputation: 63317
Of course your code should work as what you want, however I would like to use the following compact code:
List<Func<bool>> ps = new List<Func<bool>>();
ps.Add(p1);
ps.Add(p2);
ps.Add(p3);
ps.Add(p4);
ps.Add(p5);
ps.Add(p6);
ps.Add(p7);
var i = ps.Take(idcount).All(a=>a());
To use a normal for loop, you can do something like this:
var i = true;
if(ps.Count >= idcount){
for(int i = 0; i < idcount; i++){
if(!ps[i]()) { i = false; break;}
}
} //else ???
Upvotes: 2
Reputation: 148110
Using & bit wise operator will cause all the expression to be evaluated even if you get the false
in first p1()
. You should use && logical operator to combine the condition.
Binary & operators are predefined for the integral types and bool. For integral types, & computes the logical bitwise AND of its operands. For bool operands, & computes the logical AND of its operands; that is, the result is true if and only if both its operands are true. The & operator evaluates both operators regardless of the first one's value, MSDN.
Conditional-AND operator (&&) performs a logical-AND of its bool operands, but only evaluates its second operand if necessary, MSDN.
Upvotes: 1
Reputation: 51634
var areAllTrue = p1() && p2() && ... && p7();
return areAllTrue;
This will evaluate to true only if each p*() returns true.
Upvotes: 0
Reputation: 7192
Considering the invariant you've stated
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..
your code is correct.
By using '&' you'll evaluate all functions. For Instance
case 5: i = p1() & p2() & p3() & p4() & p5(); break;
here if p2()
returns false, i
will still be false but p3, p4 and p5 will be avaluated(which may not be what you want).
If you don't want that extra evaluation use the short circuit operator - &&
instead.
Upvotes: 0
Reputation: 43023
You should be using &&
which is operator for logical (true/false) values.
i = p1() && p2();
&
is a bitwise AND operator for numbers.
Upvotes: 0