Stringcomparison OrdinalIgnoreCase for true false values

It makes sense? MyValue can be "true" or "false"

If it's not Stringcomparison.OrdinalIgnoreCase?

MyValue.Equals("true", StringComparison.CurrentCultureIgnoreCase))

      

+3


source to share


3 answers


It really depends on your situation and how the rest of your program is built. From the docs on OrdinalCompareCase

The StringComparer returned by the OrdinalIgnoreCase property treats the characters in the strings for comparison as if they were converted to uppercase using the conventions of an invariant culture, and then performs a simple byte comparison that is language independent. This is most useful when comparing strings that are generated programmatically or when comparing case-insensitively for resources such as paths and file names.



Basically, if the values ​​are culture independent (generated progamously, etc.) use OrdinalIgnoreCase

0


source


I wouldn't do that. Just because a string is not equal "true"

does not mean it is equal "false"

. It's an easy way to let ugly bugs slip through. I think you need to parse the line

bool value;
if(!Boolean.TryParse(MyValue, out value)) {
    // it did not parse
}
// it parsed

      



Most likely it will be correct and it is more readable. Plus, cultural issues have just been picked up under the carpet.

+4


source


Bool.Parse

      

looks better to me.

+2


source







All Articles