Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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();
like image 252
Nate Pinchot Avatar asked Oct 22 '10 14:10

Nate Pinchot


1 Answers

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

like image 104
jason Avatar answered Sep 23 '22 15:09

jason