"if (a () && b! = null)" will "a" () "always be evaluated?

I have a code like this:

if (a() && b != null) { b.doSomething(); } 

I need a side effect of a() , even if b is null . Is this guaranteed by C #? Or can C # omit a() call if b is null ?

+47
c #
Nov 23 '11 at 11:32
source share
14 answers

Yes, a() will always be evaluated.

Since the condition evaluates from left to right, a() will always be evaluated, but b != null will only be evaluated if a() returns true .

Here is the exact specification for you, using the C # language specification version 3.0. My emphasis and decisions.

7.11.1 Logical conditional logical operators

When the operands are && or || are of type bool [...], the operation is processed as follows:

  • x ? y : false operation x && y evaluated as x ? y : false x ? y : false . In other words, x first evaluated and converted to type bool. Then , if x true , y is equally evaluated and converted to type bool, and this is the result of the operation. Otherwise, the result of the operation will be false.
+118
Nov 23 '11 at 11:39
source share

Yes, expressions are evaluated from left to right; therefore a() will always be called.

See the C # language specification (ECMA 334, clause 8.5):

With the exception of assignment operators, all binary operators are left-associative, which means that operations are performed from left to right correctly. For example, x + y + z evaluates to (x + y) + z.

+31
Nov 23 '11 at 11:34
source share

The condition is evaluated from left to right. Thus, a() always satisfied, but b may not be evaluated depending on the result of a() .

+17
Nov 23 '11 at 11:34
source share

a() will always be evaluated. b != null will only be evaluated if a() is true.

This is called a short circuit rating .

+13
Nov 23 '11 at 11:35
source share

The left side && is always evaluated. The right will be evaluated only if the left value is true. So you should be fine.

+6
Nov 23 '11 at 11:34
source share

According to MSDN :

Operation

x && y corresponds to the operation

x and y, except that if x is false, y is not evaluated because the result of AND is false regardless of the value of y. This is called a short circuit rating.

+5
Nov 23 '11 at 11:35
source share

The logical condition in your if statement consists of two logical operators: a() , which is always evaluated.

+4
Nov 23 '11 at 11:35
source share

a () is always called, since this is the first thing to check in the if statement, even if b is null.

+4
Nov 23 '11 at 11:35
source share

Boolean expressions can be fully evaluated or partially if the compiler reports that further evaluation will not change the result.

 if (a() || b()) c(); 

If you rely on the side effects of b (), you are not getting what you want if a particular compiler implementation performs an intelligent logical evaluation. I’m not sure what the standard says about evaluating Boolean expressions, but if you want to read the readability of your source code, it's best to formulate it in its entirety. This will increase readability.

 if (a()) { b(); c(); } else if (b()) c(); 
+4
Nov 23 2018-11-11T00:
source share

Since conditions are evaluated from left to right, a() will always be evaluated. And since you used the AND (& &) short circuit, if a() returns false, b != null will not be evaluated. If you want both conditions to be evaluated whether a() returns true or false, use the & operator.

+2
Nov 24 '11 at 2:02
source share

In addition to the best answer:

rating (x && y) :
if x evaluates to false , y will not evaluate.

estimate (x || y) :
if x evaluates to true , y will not evaluate.

In any case, the first operand is always evaluated. You must be very careful to have side effects in the second operand.

+2
Nov 30 '11 at 8:30
source share

Yes, but "It doesn't matter that your code works. It is important that your code is understandable . "

I prefer to do like this:

 bool val = a(); if (val && b != null) { b.doSomething(); } 
+1
Nov 30 2018-11-11T00:
source share

It depends on the priority of the required operation. Yes, this is evaluated from left to right, which means that () is always executed before the rest. If you want b! = Null to always be evaluated, swap your position.

0
Nov 30 '11 at 11:59
source share

& & condition is satisfied when both conditions are true.

So the function a () executes when b is not null.

it is basic to all pro

-5
Nov 29 '11 at 19:14
source share



All Articles