CA1309: Use ordinal StringComparison

Note

This article applies to Visual Studio 2015. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

Item Value
TypeName UseOrdinalStringComparison
CheckId CA1309
Category Microsoft.Globalization
Breaking Change Non-breaking

Cause

A string comparison operation that is nonlinguistic does not set the StringComparison parameter to either Ordinal or OrdinalIgnoreCase.

Rule Description

Many string operations, most important the System.String.Compare and System.String.Equals methods, now provide an overload that accepts a System.StringComparison enumeration value as a parameter.

When you specify either StringComparison.Ordinal or StringComparison.OrdinalIgnoreCase, the string comparison will be nonlinguistic. That is, the features that are specific to the natural language are ignored when comparison decisions are made. 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.

How to Fix Violations

To fix a violation of this rule, change the string comparison method to an overload that accepts the System.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).

When to Suppress Warnings

It is safe to suppress a warning from this rule when the library or application is intended for a limited local audience or when the semantics of the current culture should be used.

See Also

Globalization Warnings CA1307: Specify StringComparison