C# if (strValue == “FALSE” | strValue == “TRUE”)


I have a string that contains a value of either TRUE or FALSE or null, this value is read from a text file which is outside of my control. What is the best way to use '|' with 'if'? Keeping in mind strValue could also be null, if strValue is null then I don't want to enter the if statement.

if (strValue == "FALSE" | strValue == "TRUE")
  //do stuff


Best Solution

I would seriously consider using ToUpper if it's out of your control - you never know when someone changes it to "True" or "true".

if (strValue.ToUpper() == "FALSE" || strValue.ToUpper() == "TRUE") {


Regarding the responses about ToUpper and unnecessary defensive programming:

The original question said "read from a text file which is outside of my control", and "TRUE or FALSE or null". As far as I know there is no universal way of indicating NULL in text files. In CSV files, you could treat ,, as NULL or "" (empty string). In fixed width text files, you could treat all spaces as NULL, "" or "<some spaces>", for example.

So the OP has already done some data conversion/interpretation to get to this point.