What is the OR operator in an IF statement

前端 未结 11 829
轮回少年
轮回少年 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:30

    The OR operator is a double pipe:

    ||
    

    So it looks like:

    if (this || that) 
    {
      //do the other thing
    }
    

    EDIT: The reason that your updated attempt isn't working is because the logical operators must separate valid C# expressions. Expressions have operands and operators and operators have an order of precedence.

    In your case, the == operator is evaluated first. This means your expression is being evaluated as (title == "User greeting") || "User name". The || gets evaluated next. Since || requires each operand to be a boolean expression, it fails, because your operands are strings.

    Using two separate boolean expressions will ensure that your || operator will work properly.

    title == "User greeting" || title == "User name"
    

提交回复
热议问题