Use ordinal StringComparison
TypeName | UseOrdinalStringComparison |
CheckId | CA1309 |
Category | Microsoft.Globalization |
Breaking Change | Non Breaking |
A string comparison operation that is non-linguistic does not set the StringComparison parameter to either Ordinal or OrdinalIgnoreCase.
Many string operations, most important the Compare and Equals methods, now provide an overload that accepts a StringComparision enumeration value as a parameter.
When you specify either StringComparison.Ordinal or StringComparison.OrdinalIgnoreCase, the string comparison will be non-linguistic. That is, the features that are specific to the natural language are ignored when making comparison decisions. This means the decisions are based on simple byte comparisons and ignore casing or equivalence tables that are parameterized by culture. As a result, by explicitly setting the parameter to either the StringComparison.Ordinal or StringComparison.OrdinalIgnoreCase, your code often gains speed, increases correctness, and becomes more reliable.
To fix a violation of this rule, change string comparison method to an overload that accepts the StringComparison enumeration as a parameter and specify either Ordinal or OrdinalIgnoreCase. For example: change String.Compare(str1, str2) to String.Compare(str1, str2, StringComparison.Ordinal).