C# Reference ยท Collections
StringComparer class
Reference for C# StringComparer: Ordinal, OrdinalIgnoreCase, InvariantCulture and CurrentCulture comparers for sorting, dictionaries, sets and LINQ.
StringComparer is an abstract class with ready-made comparers for strings. It implements IComparer<string>, IEqualityComparer<string> and their non-generic forms, so one object works for sorting and for hashing: pass it to List<string>.Sort, new Dictionary<string, T>(comparer), new HashSet<string>(comparer), OrderBy, Distinct, GroupBy and more.
The static properties differ in how they treat case and culture. For identifiers, keys, file extensions and protocol text, use the ordinal comparers: they compare the raw UTF-16 code units and give the same answer on every machine.
var users = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase); users["Ann"] = 1; Console.WriteLine(users["ANN"]); // 1
Try it
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.
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
public static StringComparer Ordinal { get; } public static StringComparer OrdinalIgnoreCase { get; } public static StringComparer InvariantCulture { get; } public static StringComparer InvariantCultureIgnoreCase { get; } public static StringComparer CurrentCulture { get; } public static StringComparer CurrentCultureIgnoreCase { get; } public static StringComparer Create(CultureInfo culture, bool ignoreCase); public static StringComparer FromComparison(StringComparison comparisonType); public static bool IsWellKnownOrdinalComparer(IEqualityComparer<string?>? comparer, out bool ignoreCase);
| Name | Description |
|---|---|
Ordinal |
Compares UTF-16 code units; case-sensitive; fastest. |
OrdinalIgnoreCase |
Ordinal comparison after simple case folding (uppercasing with invariant rules). |
InvariantCulture, CurrentCulture |
Linguistic comparison using culture rules (accents, ligatures, culture sort order). |
...IgnoreCase variants |
The same, ignoring case. |
culture, ignoreCase |
For Create: build a comparer for a specific culture. |
Returns: Compare returns a negative, zero or positive number; Equals returns a bool; GetHashCode returns a hash consistent with Equals (equal strings give equal hashes).
Errors: GetHashCode(string) throws ArgumentNullException for null. Compare and Equals accept null. Create throws for a null culture. In this runner globalization is invariant, so culture comparers behave like invariant ones and creating a comparer for another culture such as de-DE throws.
Usage notes
- Choose
Ordinal/OrdinalIgnoreCasefor technical strings (keys, paths, header names, identifiers). Choose a culture comparer only for text the user reads, such as sorting names in a list. stringhas no default case-insensitivity:new Dictionary<string, T>()uses ordinal case-sensitive comparison, and"a" == "A"is false.- Ordinal ordering is by code unit: uppercase
A-Zsort before lowercase, and accented letters sort afterz. This is not dictionary order for human readers. StringComparer.OrdinalIgnoreCaseis faster and more predictable thanToLower()/ToUpper()followed by comparison, and it avoids allocating new strings. See string comparison.- Dictionaries and sets keep the comparer you gave them for life; check
dict.Comparerif you are unsure which one is in effect. StringComparerdoes not do natural number ordering ("file10" after "file9"); write your own comparer for that.- When you have a
StringComparisonenum value (from an API parameter),FromComparisonturns it into a comparer object.