07/10/2026
StringComparer.OrdinalIgnoreCase prevents casing bugs in .NET lookups. Without an explicit comparer, role names, tenant codes, and feature keys can behave differently than users expect.
I see this mistake in backend code because the happy path looks correct: the dictionary has the key, the set has the value, and the test uses the same casing as the seed data.
For beginners: StringComparer tells .NET how two strings should be compared. Default string lookup is case-sensitive ordinal comparison, which is not always what you want for protocol-like values.
In real projects, my suggestion is simple:
- decide whether the value is user text or a code
- use StringComparer.OrdinalIgnoreCase for stable technical codes when casing should not matter
- normalize once at the boundary when you store or log the value
- add casing tests for roles, tenants, headers, and feature keys
- never hide authorization rules inside casual string handling
This matters for clients because small casing mismatches can look like random access failures, broken tenant routing, or features that work for one user but not another.
Which rule do you prefer in API code: normalize once at the boundary, choose a comparer everywhere, or use both?