Use
StringComparison.OrdinalIgnoreCase
to perform case in-sensitive comparison 92 Console.WriteLine("String is empty!");
93 }
94
95 var myCmp = s1.ToLower() == s2.ToLower(); 96
97 object o = new Foo();
98 var foo = o as Foo;
Description
While converting string
s to lower/upper case and then comparing might work to perform a case insensitive comparison, the safer, reliable, and performant alternative is to invoke the string.Equals
method while specifying the StringComparison.OrdinalIgnoreCase
enum.
Examples
Bad Practice
var areEqual = str1.ToLower() == str2.Lower();
Recommended
var areEqual = string.Equals(str1, str2, StringComparison.OrdinalIgnoreCase);