Use
StringComparison.OrdinalIgnoreCase
to perform case in-sensitive comparison 65 // CS-R1017: Inefficient case insensitive comparison.
66 // Should be rewritten as `string.Equals(s1, s2, StringComparison.OrdinalIgnoreCase)`
67 // https://deepsource.io/directory/analyzers/csharp/issues/CS-R1017
68 var caseInsensitiveCmp = s1.ToLower() == s2.ToLower(); 69
70 // CS-R1045: Explicit array size is redundant when initialized in place.
71 // https://deepsource.io/directory/analyzers/csharp/issues/CS-R1045
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);