Does the compiler continue evaluating an expression where all must be true if the first is false?

耗尽温柔 提交于 2019-11-28 09:05:29

问题


I'm sure this question has probably been answered before, so I apologize, 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();

回答1:


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 |.




回答2:


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 &.




回答3:


No

The conditional-AND operator (&&) performs a logical-AND of its bool operands, but only evaluates its second operand if necessary




回答4:


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.




回答5:


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.




回答6:


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.



来源:https://stackoverflow.com/questions/3997612/does-the-compiler-continue-evaluating-an-expression-where-all-must-be-true-if-th

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!