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.
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 operationx && ycorresponds to the operationx & y, except thatyis evaluated only ifxistrue
...
The operationx && yis evaluated as(bool)x ? (bool)y : false. In other words,xis first evaluated and converted to typebool. Then, ifxistrue,yis evaluated and converted to typebool, and this becomes the result of the operation. Otherwise, the result of the operation isfalse.
(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 && yis evaluated asT.false((T)x) ? (T)x : T.&((T)x, y), whereT.false((T)x)is an invocation of theoperator falsedeclared inT, andT.&((T)x, y) is an invocation of the selectedoperator &. In addition, the value (T)x shall only be evaluated once.In other words,
xis first evaluated and converted to typeTandoperator falseis invoked on the result to determine ifxis definitelyfalse.
Then, ifxis definitelyfalse, the result of the operation is the value previously computed forxconverted to typeT.
Otherwise,yis evaluated, and the selected operator&is invoked on the value previously computed forxconverted to typeTand the value computed foryto produce the result of the operation.
(C# Language Specification Version 4.0 - 7.12.2 User-defined conditional logical operators)
false operator on the left side returns true and returns the left side instead, and else invokes the custom & operator. So it mimics the short circuiting behavior bools have. The typical application of this is an extended bool that can have a third state. For example you could implement something similar to DBBol or bool? with that.&&= and ||= operators.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.
AndAlso and OrElse, not on And and Or, which is likely what 90% of people always use.I know I'm late to the party, but in C# 6.0 you can do this too:
if(myObj?.SomeString != null) Which is the same thing as above.
Also see: What does question mark and dot operator ?. mean in C# 6.0?
sure, it's safe on C#, if the first operand is false then the second is never evaluated.
It is perfectly safe. C# is one of those languages.
In C#, && and || are short-circuited, meaning that the first condition is evaluated and the rest is ignored if the answer is determined.
In VB.NET, AndAlso and OrElse are also short-circuited.
In javaScript, && and || are short-circuited too.
I mention VB.NET to show that the ugly red-headed step-child of .net also has cool stuff too, sometimes.
I mention javaScript, because if you are doing web development then you probably might also use javaScript.
a || b is equivalent to a ? a : b, and a && b is equivalent to a ? b : a, except that a is evaluated only once. This is more noticeable in JavaScript, where operands of arbitrary type can be used. Other languages such as C don't follow this principle - they accept operands of non-boolean types but always return a boolean value.Yes, C# and most languages compute the if sentences from left to right.
VB6 by the way will compute the whole thing, and throw an exception if it's null...
And doesn't short-curcuit, but & doesn't either. (Perhaps 2+3 are because you confuse parsing with something that happens at runtime...)While C# && short circuiting is part of the spec, the similar C# operator and and other pattern matching operators do not specify an evaluation order, and the compiler is free to do them in whatever order it wants. See the note on this page