Or versus OrElse

前端 未结 8 1564
感情败类
感情败类 2020-11-27 04:33

What\'s the difference between or and OrElse?

if temp is dbnull.value or temp = 0

produces the error:

8条回答
  •  有刺的猬
    2020-11-27 04:54

    (I've looked at other answers and realized I was terribly wrong)

    The OrElse operator "performs short-circuiting logical disjunction on two expressions", that is to say: if the left operand is true and so the entire expression is guaranteed to be true the right operand won't even be evaluated (this is useful in cases like:

    string a;
    //...
    if (a is null) or (a = "Hi") //...
    

    to avoid a NullReferenceException throw by the right-hand operand.

    I'm sincerely astonished that this (lazy evaluation) isn't the default behaviour of or and and as it is in C/C++ and C# (and many other languages...)

提交回复
热议问题