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), where
T.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)