Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Short circuiting statement evaluation -- is this guaranteed? [C#]

Quick question here about short-circuiting statements in C#. With an if statement like this:

if (MyObject.MyArray.Count == 0 || MyObject.MyArray[0].SomeValue == 0)
{

//....
}

Is it guaranteed that evaluation will stop after the "MyArray.Count" portion, provided that portion is true? Otherwise I'll get a null exception in the second part.

like image 813
larryq Avatar asked Apr 22 '10 15:04

larryq


People also ask

Does C have short-circuit evaluation?

In imperative language terms (notably C and C++), where side effects are important, short-circuit operators introduce a sequence point – they completely evaluate the first argument, including any side effects, before (optionally) processing the second argument.

What is short-circuiting in expression evaluation?

Short-Circuit Evaluation: Short-circuiting is a programming concept in which the compiler skips the execution or evaluation of some sub-expressions in a logical expression. The compiler stops evaluating the further sub-expressions as soon as the value of the expression is determined.

Are logical operators in C evaluated with shortcut evaluation?

Do logical operators in the C language are evaluated with the short circuit? Explanation: None.

What is short-circuit evaluation in decision making expressions in C#?

What is meant by short-circuit evaluation in decision making expressions in C#? In compound conditional tests involving AND/OR logic, the conditional Boolean expressions are evaluated only as much as necessary to determine whether the entire expression is true or false. This feature is called short-circuit evaluation.


4 Answers

Yes, this is guaranteed.

C# Language Specification - 7.11 Conditional logical operators:

The && and || operators are called the conditional logical operators. They are also called the "short-circuiting" logical operators.

Therefore they will support logical short-circuiting by definition - you can rely on this behavior.

Now it is important to make a distinction between a conditional operator and a logical operator:

  • Only conditional operators support short-circuiting, logical operators do not.
  • C#'s logical operators look just like their conditional counterparts but with one less character so a logical OR is | and a logical AND is &.
  • Logical operators can be overloaded but conditional operators cannot (this is a bit of an technicality as conditional operator evaluation does involve overload resolution and this overload resolution can resolve to a custom overload of the type's logical operator, so you can work around this limitation to a certain extent).
like image 175
Andrew Hare Avatar answered Oct 02 '22 13:10

Andrew Hare


Yes, it is guaranteed.

http://msdn.microsoft.com/en-us/library/6373h346%28v=VS.80%29.aspx

The conditional-OR operator (||) performs a logical-OR of its bool operands, but only evaluates its second operand if necessary.

like image 31
Bryan Watts Avatar answered Oct 02 '22 13:10

Bryan Watts


Yes, it is guaranteed, but you can still get a null reference exception if MyArray is null (or MyObject for that matter obviously).

like image 33
kemiller2002 Avatar answered Oct 02 '22 12:10

kemiller2002


Just a small observation.

You said this:

Otherwise I'll get a null exception in the second part. (emphases mine)

This isn't true, actually. If short-circuiting weren't guaranteed, you could get an IndexOutOfRangeException in the second part.

It's still possible you could get a NullReferenceException, if the first item in your MyArray object is actually null (or if any of the other objects in that expression are).

The only totally safe check would be this:

bool conditionHolds =
    MyObject == null ||
    MyObject.MyArray == null ||
    MyObject.MyArray.Count == 0 ||
    MyObject.MyArray[0] == null ||
    MyObject.MyArray[0].SomeValue == 0;

if (conditionHolds)
{
    //....
}
like image 24
Dan Tao Avatar answered Oct 02 '22 14:10

Dan Tao