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

后端 未结 8 1513
梦毁少年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:45

    The conditional OR operator || is what you need

    if ((Boolean.Parse(staff.getValue("Male")) || Boolean.Parse(staff.getValue("Female")))
    {
       //is true
    }
    

    If the first condition is TRUE, then the second condition isn't checked since the outcome is obviously going to return TRUE.

    0 讨论(0)
  • 2021-01-12 13:45

    This is a similar scenario but I am checking for three or more bool values.

    Thread th = new Thread(() =>
                    {
                        while (true)
                        {
                            bool allReadComplete = true;
    
                            foreach (IDataProvider provider in lstDataProviders)
                            {
                                provider.StartReading();
    
                                if (provider.FinishedReading)
                                  allReadComplete = allReadComplete && provider.FinishedReading;
                                else
                                  allReadComplete = provider.FinishedReading;
                            }
    
                            // to induce some context switching
                            Thread.Sleep(0);
    
                            if (allReadComplete)
                                break;
                        }
    
                        Console.WriteLine("Thread Exiting");
    
                    });
                th.IsBackground = true;
                th.Start();
    
    0 讨论(0)
  • 2021-01-12 13:49

    If EXACTLY ONE should be true then it is:

    var male = bool.Parse(staff.getValue("Male"));
    var female = bool.Parse(staff.getValue("Female"));
    
    if (male ^ female)    
    {
       //is true
    }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-12 13:54

    A little exception checking is needed anyway. The Boolean.Parse() method gets a string as argument and returns either true or false only if the argument, once stripped out of whitespace, is equal to "True" or "False" (note capitalization). In ANY other case the function returns an exception.

    Supposing that the possible values of staff.getValue("Male") and staff.getValue("Female") are exactly those two, then the simple disjunction (||) is sufficient. If any other return value is possible, including null and the empty string, then you have to check for exceptions

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

    or compare manually

    bool isMale = Boolean.TrueValue == staff.getValue("Male");
    bool isFemale = Boolean.TrueValue == staff.getValue("Female");
    if (isMale || isFemale) // note double pipe ||
    {
        // do something if true
    }
    
    0 讨论(0)
  • 2021-01-12 13:57

    Sounds like you're looking for the logical OR.

    if(condition1 || condition2)
    {
    }
    
    0 讨论(0)
提交回复
热议问题