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

后端 未结 6 2287
不思量自难忘°
不思量自难忘° 2020-12-10 11:53

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

if (MyObject.MyArray.Count == 0 || MyObject.MyArray[0].SomeValu         


        
6条回答
  •  忘掉有多难
    2020-12-10 12:28

    Yes,

    For AND operations if any of the operand evaluated to false then total expression evaluated to false then there is no need to evaluate remaining expressions, And in case of OR operation if any of the operand evaluated to true then remaining evaluation can be skipped

    So by using && or || operator, whole expression can be evaluated to true or false without evaluating all sub expressions.

    But consider its side effect also. This article might be helpful to understand short-circuit evaluation in depth with some real world examples.

提交回复
热议问题