3 out of 6 rated this helpful - Rate this topic

StringComparison Enumeration

Note: This enumeration is new in the .NET Framework version 2.0.

Specifies the culture, case, and sort rules to be used by certain overloads of the String.Compare and String.Equals methods.

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public enum StringComparison
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public enum StringComparison
SerializableAttribute 
ComVisibleAttribute(true) 
public enum StringComparison
  Member name Description
Supported by the .NET Compact Framework CurrentCulture Compare strings using culture-sensitive sort rules and the current culture. 
Supported by the .NET Compact Framework CurrentCultureIgnoreCase Compare strings using culture-sensitive sort rules, the current culture, and ignoring the case of the strings being compared. 
Supported by the .NET Compact Framework InvariantCulture Compare strings using culture-sensitive sort rules and the invariant culture. 
Supported by the .NET Compact Framework InvariantCultureIgnoreCase Compare strings using culture-sensitive sort rules, the invariant culture, and ignoring the case of the strings being compared. 
Supported by the .NET Compact Framework Ordinal Compare strings using ordinal sort rules. 
Supported by the .NET Compact Framework OrdinalIgnoreCase Compare strings using ordinal sort rules and ignoring the case of the strings being compared. 

The StringComparison enumeration is used to specify whether a string comparison should use the current culture or the invariant culture, word or ordinal sort rules, and be case-sensitive or case-insensitive.

An operation that uses word sort rules performs a culture-sensitive comparison wherein certain nonalphanumeric Unicode characters might have special weights assigned to them. Using word sort rules and the conventions of a specific culture, the hyphen ("-") might have a very small weight assigned to it so that "coop" and "co-op" appear next to each other in a sorted list.

An operation that uses ordinal sort rules performs a comparison based on the numeric value (Unicode code point) of each Char in the string. An ordinal comparison is fast, but culture-insensitive. Using ordinal sort rules a string that starts with some Unicode character, U+xxxx, comes before a string that starts with some Unicode character, U+yyyy, if the value of xxxx is numerically less than yyyy.

For more information about comparisons, see the System.String class remarks. For more information about culture, see the System.Globalization.CultureInfo class remarks.

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0

.NET Compact Framework

Supported in: 2.0
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Please specify what kind of comparison you expect :) Use StringComparison (or CultureInfo)!

Please specify what kind of comparison you expect :)  Use StringComparison or CultureInfo if necessary.

Some applications need linguistic comparisons, others need ordinal type comparisons.  A very general rule would be that user facing comparisons would need linguistic comparisons (use current culture), and security comparisons may need ordinal or ordinal ignore case.  For some applilcations the default comparisons will not be appropriate.  It is much better to understand the differences and use the correct version.

Additionally note that it is challenging to try to figure out what is going to be a match for the file system, URI/IRIs, etc.  It is generally preferable to use the specific comparisons for the resource you're trying to compare.  For example ?, & and other escaping rules in URLs, ..\, .\, :, etc in file names, etc. could all cause unexpected behavior if not properly tested for.