Visual Studio Team System
Use ordinal StringComparison

TypeName

UseOrdinalStringComparison

CheckId

CA1309

Category

Microsoft.Globalization

Breaking Change

Non Breaking

Cause

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

Rule Description

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.

How to Fix Violations

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).

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

Reference

Other Resources

Tags :


Community Content

David M. Kean - MSFT
Example

The following example shows a method that incorrectly compares two string values using an invariant comparison.

[C#]
  
using System;
  
namespace Samples
{
class Program
{
static void Main(string[] args)
{
if (string.Equals("ß", "SS", StringComparison.InvariantCultureIgnoreCase))
{
Console.WriteLine("The values are equal!");
}
else
{
Console.WriteLine("The values are not equal!");
}
}
}
}

The above outputs the following:

The values are equal!


The two values are considered equal because linguisitic casing is being taken into consideration.

To fix the above violation, replace the invariant comparison with an ordinal comparison.

The following example shows this.

[C#]
  
using System;
  
namespace Samples
{
class Program
{
static void Main(string[] args)
{
if (string.Equals("ß", "SS", StringComparison.OrdinalCultureIgnoreCase))
{
Console.WriteLine("The values are equal!");
}
            else
{
Console.WriteLine("The values are not equal!");
}
}
}
}


The above outputs the following:

The values are not equal!
Tags :

David M. Kean - MSFT
More information

For more information on comparing strings, see the following article:

New Recommendations for Using Strings in Microsoft .NET 2.0
http://msdn2.microsoft.com/en-us/library/ms973919.aspx

Tags :

Page view tracker