In .NET, which StringComparison value should you pass when matching usernames, file extensions or protocol tokens, and which for text shown to people?
answer
- identifiers versus human language
- compare code units, apply no rules
- case-insensitive tokens get a variant
- culture belongs to displayed text
- invariant is still linguistic
basics
~10 sUse StringComparison.Ordinal, or OrdinalIgnoreCase when case should not matter, for usernames, file extensions, paths, keys and protocol tokens. Use CurrentCulture or CurrentCultureIgnoreCase only for text people read, such as sorting a displayed list.
solid answer
~40 sAsk what the string *is*. If it is an identifier the program matches (a username, an email domain, a file extension, a header name, a dictionary key), compare it with `StringComparison.Ordinal`, or `OrdinalIgnoreCase` when the format is case-insensitive. Ordinal compares UTF-16 code units, so the answer is exact, fast and the same on every machine. If the string is language a person reads, such as names in a list or search text, use `CurrentCulture` or `CurrentCultureIgnoreCase` so it sorts and matches by that person's rules. `InvariantCulture` is not a neutral middle ground: it is still a linguistic comparison, just with a fixed culture, and it fits persisted data that must sort the same everywhere. And never fake case-insensitivity with `a.ToLower() == b.ToLower()`: it allocates two strings and lowercases with the current culture.
code
csharp · 24 linesusing System.Globalization;
CultureInfo.CurrentCulture = new CultureInfo("tr-TR");
string upload = "AVATAR.PNG";
string username = "Iris";
string[] displayNames = { "Zeynep", "ışık", "Ilker", "irem" };
// File extension: a non-linguistic token, case does not matter.
bool isPng = upload.EndsWith(".png", StringComparison.OrdinalIgnoreCase);
// Username lookup: an identifier, case does not matter.
bool sameUser = string.Equals(username, "IRIS", StringComparison.OrdinalIgnoreCase);
// The same check with the current culture: Turkish rules apply.
bool sameUserCulture = string.Equals(username, "IRIS", StringComparison.CurrentCultureIgnoreCase);
// A list shown to Turkish users: sort by their language's rules.
Array.Sort(displayNames, StringComparer.CurrentCulture);
Console.WriteLine($"isPng={isPng} sameUser={sameUser} sameUserCulture={sameUserCulture}");
Console.WriteLine(string.Join(", ", displayNames));
Array.Sort(displayNames, StringComparer.Ordinal);
Console.WriteLine(string.Join(", ", displayNames));go deeper
Know the two ordinal values and when they apply: identifiers, file extensions and keys get Ordinal or OrdinalIgnoreCase, and you pass the enum instead of lowercasing both strings.
Explain what ordinal actually compares (UTF-16 code units, with invariant case folding for IgnoreCase) and why InvariantCulture is still linguistic rather than neutral.
Map real data to the right value without hesitation, and point out where a culture-aware default would make the same input behave differently on two servers.
Frame it as a codebase rule: ordinal by default, culture only for text shown to people, and an explicit comparison at every call site so intent is reviewable.
## The six values `System.StringComparison` is the enum that most .NET string APIs accept to say *how* two strings are compared. It has six members, in three pairs: | Value | Kind | Case | What decides the result | |---|---|---|---| | `Ordinal` | non-linguistic | sensitive | the numeric values of the UTF-16 code units | | `OrdinalIgnoreCase` | non-linguistic | insensitive | code units after invariant case folding | | `InvariantCulture` | linguistic | sensitive | Unicode collation rules with a fixed, culture-neutral table | | `InvariantCultureIgnoreCase` | linguistic | insensitive | the same, ignoring case | | `CurrentCulture` | linguistic, culture-aware | sensitive | the rules of `CultureInfo.CurrentCulture` | | `CurrentCultureIgnoreCase` | linguistic, culture-aware | insensitive | the same, ignoring case by that culture's rules | You pass one to `string.Equals(a, b, comparisonType)`, `string.Compare(a, b, comparisonType)`, `StartsWith`, `EndsWith`, `IndexOf`, `Contains` and `Replace`, and the matching `StringComparer` property (`StringComparer.Ordinal`, `StringComparer.CurrentCulture` and so on) to collections and sorting. ## Ordinal: exact and predictable An **ordinal** comparison walks both strings `char` by `char` and compares the numbers. It applies no language rules at all: - `"dog"` and `"Dog"` differ under `Ordinal` and match under `OrdinalIgnoreCase`. - `"resume"` and `"résumé"` differ under both: an accented letter is a different code unit, and ignoring case does not ignore accents. - `"Á"` and `"á"` match under `OrdinalIgnoreCase`, because case folding outside ASCII uses the invariant culture's casing table. - The result never depends on the machine's culture settings, so the same input gives the same answer on a laptop, a container and a server in another region. The .NET guidance on string comparison calls `Ordinal` and `OrdinalIgnoreCase` the safe default for culture-agnostic matching, and it is also the fastest option because no collation work is done. ## Culture-aware: the rules of a language A **linguistic** comparison breaks strings into collation elements and compares what they *mean* in a language. Since .NET 5 it uses the ICU library on every supported platform. That is what you want for text people read: a Swedish user expects "å" to sort after "z", a Turkish user expects "ı" and "i" to be different letters. It is the wrong tool for identifiers, because the same two strings can compare differently on two servers, and because linguistic rules ignore some characters entirely (a soft hyphen or a zero-width joiner can be skipped), so strings that differ in their stored code units can compare equal. `InvariantCulture` is linguistic too. It uses a fixed culture-neutral table, so it gives the same answer everywhere, but it still treats `"é"` and `"e"` plus a combining accent as equal and still skips zero-weight characters. Its real use is narrow: persisted, linguistically meaningful data that has to sort identically for every user. ## Choosing for real data For a user-account service, the mapping is short: 1. **Usernames and role names** the program matches: `OrdinalIgnoreCase` if sign-in is case-insensitive, `Ordinal` if it is not. 2. **Email domains, file extensions, URL schemes, HTTP header names**: `OrdinalIgnoreCase`; these formats define case-insensitivity over ASCII, not over a language. 3. **Passwords, tokens, hashes, case-sensitive keys**: `Ordinal` (and for secrets, a constant-time comparison on top). 4. **A list of display names shown to a user, or search text they typed**: `CurrentCulture` or `CurrentCultureIgnoreCase`, or a specific `CultureInfo` for that user. 5. **A sorted file shipped with the app that must merge the same everywhere**: `InvariantCulture`. ## Why not lowercase both strings? `a.ToLower() == b.ToLower()` looks like a case-insensitive comparison, but it has three problems: - **It allocates** two new strings on every call; `string.Equals(a, b, StringComparison.OrdinalIgnoreCase)` allocates nothing. - **`ToLower()` uses the current culture.** Under `tr-TR`, `"I".ToLower()` is the dotless `"ı"`, so `"ADMIN"` no longer matches `"admin"`. - **It hides intent.** A reader cannot tell whether linguistic or ordinal behaviour was meant. If you need a normalized copy, for example to store as a key, the .NET guidance recommends `ToUpperInvariant()`; for a comparison, pass the `StringComparison` instead. ## Takeaway The question to ask is not "which comparison is correct" in the abstract but "is this string an identifier or language?". Identifiers get ordinal; language shown to a person gets the culture; the invariant culture is for the rare data that is linguistic but must behave the same for everyone.
- Why not use InvariantCultureIgnoreCase for identifiers, since it gives the same result on every server?Because it is still a linguistic comparison. It treats `"é"` and `"e"` plus a combining accent as equal and skips zero-weight characters such as a soft hyphen, so two identifiers with different stored code units can match, and it does more work than ordinal. `OrdinalIgnoreCase` compares exactly what was stored and is also identical on every server.
- Is OrdinalIgnoreCase the same as calling ToUpperInvariant() on both strings and comparing ordinally?Behaviourally, yes: the .NET docs describe `OrdinalIgnoreCase` as that composition, but it does the folding while comparing, without allocating the two upper-cased copies. So `"Á"` matches `"á"`, `"a"` does not match `"á"`, and the Turkish dotless `"ı"` does not match `"I"`, because the invariant table does not map it.
- Which comparison should a people-search box use when the user types a name?A culture-aware one, ideally with the user's own `CultureInfo` rather than the server's: `string.Compare(a, b, culture, CompareOptions.IgnoreCase)`, adding `CompareOptions.IgnoreNonSpace` if accents should not matter. This is text a person typed and expects to match by their language's rules, which ordinal comparison does not know.
- InvariantCulture is the culture-neutral, exact choice for identifiers and keys.
- Calling ToLower() on both strings is the standard case-insensitive comparison.
- Ordinal comparison is only a performance trick and never changes the result.
- File names can use CurrentCulture because every server runs an English locale.
- OrdinalIgnoreCase ignores accents, so resume equals résumé.