Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is relying on && short-circuiting safe in .NET?

Assume myObj is null. Is it safe to write this?

if(myObj != null && myObj.SomeString != null) 

I know some languages won't execute the second expression because the && evaluates to false before the second part is executed.

like image 917
patrick Avatar asked Jan 27 '11 19:01

patrick


People also ask

How do you use relying?

Relying sentence example. Antonio, relying on the popular hostility to a Spanish ruler, presented himself as a candidate.

What is another word for relying on?

In this page you can discover 8 synonyms, antonyms, idiomatic expressions, and related words for rely on, like: depend on, count on, reckon on, believe in, trust, depend upon, bank on and rely upon.

Is rely on a preposition?

rely on/upon somebody/something for something As babies, we rely entirely on others for food. rely on/upon somebody/something to do something These days we rely heavily on computers to organize our work. rely on/upon somebody/something doing something The industry relies on the price of raw materials remaining low.

Is rely on a verb?

verb (used without object), re·lied, re·ly·ing. to depend confidently; put trust in (usually followed by on or upon): You can rely on her work.


2 Answers

Yes. In C# && and || are short-circuiting and thus evaluates the right side only if the left side doesn't already determine the result. The operators & and | on the other hand don't short-circuit and always evaluate both sides.

The spec says:

The && and || operators are called the conditional logical operators. They are also called the “shortcircuiting” logical operators.
...
The operation x && y corresponds to the operation x & y, except that y is evaluated only if x is true
...
The operation x && y is evaluated as (bool)x ? (bool)y : false. In other words, x is first evaluated and converted to type bool. Then, if x is true, y is evaluated and converted to type bool, and this becomes the result of the operation. Otherwise, the result of the operation is false.

(C# Language Specification Version 4.0 - 7.12 Conditional logical operators)

One interesting property of && and || is that they are short circuiting even if they don't operate on bools, but types where the user overloaded the operators & or | together with the true and false operator.

The operation x && y is evaluated as T.false((T)x) ? (T)x : T.&((T)x, y), where T.false((T)x) is an invocation of the operator false declared in T, and T.&((T)x, y) is an invocation of the selected operator &. In addition, the value (T)x shall only be evaluated once.

In other words, x is first evaluated and converted to type T and operator false is invoked on the result to determine if x is definitely false.
Then, if x is definitely false, the result of the operation is the value previously computed for x converted to type T.
Otherwise, y is evaluated, and the selected operator & is invoked on the value previously computed for x converted to type T and the value computed for y to produce the result of the operation.

(C# Language Specification Version 4.0 - 7.12.2 User-defined conditional logical operators)

like image 168
CodesInChaos Avatar answered Sep 19 '22 19:09

CodesInChaos


Yes, C# uses logical short-circuiting.

Note that although C# (and some other .NET languages) behave this way, it is a property of the language, not the CLR.

like image 38
harpo Avatar answered Sep 22 '22 19:09

harpo