Skip to content
Tutorials Center
My learning

Topics

C# Reference Β· Strings and Text

The StringComparison enum

Reference for the C# StringComparison enum: Ordinal, OrdinalIgnoreCase, CurrentCulture, InvariantCulture, which to choose, and which APIs accept it.

StringComparison is an enum in System that tells string methods how to compare text. It has six members: three culture-sensitive (CurrentCulture, CurrentCultureIgnoreCase, InvariantCulture, InvariantCultureIgnoreCase) and two ordinal (Ordinal, OrdinalIgnoreCase).

Ordinal comparison looks at raw character values, like comparing numbers. Culture comparison follows the linguistic rules of a language, where for instance accents, case, and special letters affect order and equality. Passing the argument explicitly makes your code correct, readable and independent of the machine it runs on.

C#
bool same = "Index".Equals("INDEX", StringComparison.OrdinalIgnoreCase);
int pos = text.IndexOf("end", StringComparison.Ordinal);

Try it

C# Ordinal and ignoring case

Press Run (or Ctrl + Enter) to see the result here.

Program input Β· what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# All members and the comparer equivalents

Press Run (or Ctrl + Enter) to see the result here.

Program input Β· what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Syntax

C#
public enum StringComparison
{
    CurrentCulture = 0,
    CurrentCultureIgnoreCase = 1,
    InvariantCulture = 2,
    InvariantCultureIgnoreCase = 3,
    Ordinal = 4,
    OrdinalIgnoreCase = 5
}
Member Description
Ordinal Compares UTF-16 code unit values. Case-sensitive, fastest.
OrdinalIgnoreCase Ordinal, after simple case mapping. Fast; the right choice for identifiers, file names and protocol text.
CurrentCulture Linguistic rules of the thread's current culture. For text shown to a person in their language.
CurrentCultureIgnoreCase The same, ignoring case.
InvariantCulture Linguistic rules of the invariant culture, stable across machines.
InvariantCultureIgnoreCase The same, ignoring case.

Accepted by: string.Equals, Compare, StartsWith, EndsWith, Contains, IndexOf, LastIndexOf, Replace, string.GetHashCode, MemoryExtensions span methods, and StringComparer.FromComparison.

Errors / undefined behavior: an undefined numeric value throws ArgumentException (ArgumentOutOfRangeException in some overloads).

Usage notes

  • Defaults differ by method. ==, Equals(string), Contains(string) and Replace(string, string) are ordinal. Compare, CompareTo, StartsWith(string), EndsWith(string) and IndexOf(string) are culture-sensitive. A call without the argument may behave differently from what you expect.
  • Rule of thumb: Ordinal or OrdinalIgnoreCase for anything a program consumes; CurrentCulture for sorting and searching text a user reads.
  • InvariantCulture is rarely the answer. It is stable, but it is still linguistic and slower than ordinal. Use it only for persisted data that must sort linguistically in the same way everywhere.
  • Culture can surprise. In some cultures "ae" compares equal to "Γ¦", and "\0" may be ignored entirely. In Turkish, i and I are not a case pair. Ordinal is immune to this.
  • Hash-based collections need a matching comparer: new Dictionary<string, T>(StringComparer.OrdinalIgnoreCase). The comparison type is fixed when the collection is created.
  • Runner note. This site's runner uses invariant globalization, so the culture members behave much like ordinal ones. The differences described above are what you would see on a normal machine; test with real cultures locally.
  • Analyzers (CA1307, CA1309, CA1310, CA1862) warn about calls that omit the argument. Turn them on.