views:

121

answers:

6

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();
+2  A: 

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.

Justin Niessner
`||` does not stop evaluating when it detects an argument that is false.
Jason
@Malfist: I don't need one. You're telling me that `false || true` should evaluate to `false`?
Jason
@Jason Wow. Made a quick edit and obviously didn't think about what I was typing. Rolled back. ...need more coffee.
Justin Niessner
@Jason, my bad. I misread that.
Malfist
+2  A: 

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

Pablo Santa Cruz
+6  A: 

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

Jason
m.edmondson
Matthew Vines
+1  A: 

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.

DanDan
Chris Marisic
Yes you're right, I was thinking of C++.
DanDan
+2  A: 

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.

Will Marcouiller