Does the compiler continue evaluating an expression where all must be true if the first is false?
I'm sure this question has probably been answered before, so I apologiz开发者_如何转开发e, but I wasn't able to find the proper search terms to find the answer.
Given the following code example, does db.GetRecords().Any()
get executed?
string s = "Z";
bool x = s.IndexOfAny(new[] { 'A', 'B' }) > 0 &&
db.GetRecords().Any();
No. Both &&
and ||
are evaluated by short-circuit evaluation. This means that a && b
returns false if a
is false and a || b
returns true if a
is true and it will not evaluate b
in either of these cases.
If for some reason you do not want short-circuit evaluation you can use the bitwise operators &
and |
.
No, C# use short circuit and. So the answer is no.
If you need to evaluate both, use NON-SHORT-CIRCUIT operator by using just one ampersand &.
tring s = "Z";
bool x = s.IndexOfAny(new[] { 'A', 'B' }) > 0 &
db.GetRecords().Any();
Please note the single &.
No
The conditional-AND operator (&&) performs a logical-AND of its bool operands, but only evaluates its second operand if necessary
No. The &&
operator short-circuits (which means it stops evaluating the expression after any part of the expression evaluates to false).
The ||
operator also short-circuits but stops evaluating after any part of the expression evaluates to true.
I say that for a C# logic-AND (&&
), the second that an expression is false, since they all need to be true for the expression to be true, the compiler stops evaluating immediately.
&& Operator (C# Reference)
The conditional-AND operator (&&) performs a logical-AND of its bool operands, but only evaluates its second operand if necessary.
Contrarely to a logic-AND, the logic-OR (||
) only requires only one expression among all to be true, for the whole expression to be true. So, instead of short-circuiting over a false
evaluation, the ||
operator causes the compiler to short-circuit over a true evaluation.
|| Operator (C# Reference)
Now, that is the behaviour of the C# compiler, but it doesn't mean every compiler bahves that way, as in VB.NET, you have two logic-AND operators (And
, AndAlso
), and two logic-OR operators (Or
, OrElse
). The And
operator, used for both bitwise and logic conjunctions, does not short-circuit when the first expression returns false and evaluate the other anyway, while AndAlso
will short-circuit the evaluation when the first logical expression is false. That is the same with Or
and OrElse
, where Or
doesn't short-circuit, and OrElse
does.
And Operator (Visual Basic)
Performs a logical conjunction on two Boolean expressions, or a bitwise conjunction on two numeric expressions.
AndAlso Operator (Visual Basic)
Performs short-circuiting logical conjunction on two expressions.
Or Operator (Visual Basic)
Performs a logical disjunction on two Boolean expressions, or a bitwise disjunction on two numeric expressions.
OrElse Operator (Visual Basic)
Performs short-circuiting inclusive logical disjunction on two expressions.
In short, I would say that this depends on the compiler you're working with. As for C#, it does short-circuit.
It is shortcircuiting and allows you to do things like this:
if(ob && ob.somefunc()) { ... }
if both operations were evaluated, there would be a possibility of referencing a null object, which would be a runtime exception.
精彩评论