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

JasonW picture JasonW · Aug 19, 2010 · Viewed 11.4k times · Source

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 ((Boolean.Parse(staff.getValue("Male")) | Boolean.Parse(staff.getValue("Female")))    
{   
   // is true
}

Is this correct?

Thanks

Answer

Lasse Espeholt picture Lasse Espeholt · Aug 19, 2010

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
}