C# How do I check if one of two values is TRUE?

后端 未结 8 1516
梦毁少年i
梦毁少年i 2021-01-12 13:24

Should be a simple question for the C# experts here.

I basically want to check if one value or another is TRUE, a wild stab at the code is below:

if          


        
8条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-12 13:52

    Use the || (double pipe), logical OR.

    bool isMale = Boolean.Parse(staff.getValue("Male");
    bool isFemale = Boolean.Parse(staff.getValue("Female");
    if (isMale || isFemale) // note double pipe ||
    {
       // do something if true
    }
    

    In C# statement expressions are evaluated from left to right. In an OR operation, the second expression will not be evaluated if the first one equals true.

提交回复
热议问题