Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2008
Development Edition
 Use ordinal StringComparison
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
Visual Studio Team System
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).

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.

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Example      David M. Kean - MSFT   |   Edit   |   Show History

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 What's this?: Add a tag
Flag as ContentBug
More information      David M. Kean - MSFT   |   Edit   |   Show History

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 What's this?: Add a tag
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker