What is the OR operator in an IF statement

前端 未结 11 833
轮回少年
轮回少年 2020-12-14 05:50

In C#, how do I specify OR:

if(this OR that) {do the other thing}

I couldn\'t find it in the help.

Update:

11条回答
  •  隐瞒了意图╮
    2020-12-14 06:37

    Just for completeness, the || and && are the conditional version of the | and & operators.

    A reference to the ECMA C# Language specification is here.

    From the specification:

    3 The operation x || y corresponds to the operation x | y, except that y is evaluated only if x is false.

    In the | version both sides are evaluated.

    The conditional version short circuits evaluation and so allows for code like:

    if (x == null || x.Value == 5)
        // Do something 
    

    Or (no pun intended) using your example:

    if (title == "User greeting" || title == "User name") 
        // {do stuff} 
    

提交回复
热议问题