Is there a C case insensitive equals operator?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
C# does not have a case-insensitive equals operator. The == operator and string.Equals() are case-sensitive by default. For case-insensitive comparison, use string.Equals(a, b, StringComparison.OrdinalIgnoreCase) for the best combination of performance and correctness. For culture-aware comparisons (sorting, display), use StringComparison.CurrentCultureIgnoreCase. Avoid ToLower()/ToUpper() for comparison — they allocate new strings and have edge cases with certain Unicode characters.
Recommended Approach: StringComparison
StringComparison.OrdinalIgnoreCase performs a byte-level comparison after case folding, making it the fastest option for non-linguistic comparisons like file paths, identifiers, and configuration keys.
StringComparison Options
| Option | Use Case | Performance |
OrdinalIgnoreCase | Identifiers, paths, keys | Fastest |
CurrentCultureIgnoreCase | User-facing text in current locale | Medium |
InvariantCultureIgnoreCase | Culture-independent linguistic comparison | Medium |
Common Patterns
Switch Statement (C# 7+)
Contains, StartsWith, EndsWith
Dictionary with Case-Insensitive Keys
LINQ Queries
HashSet with Case-Insensitive Comparison
Why Not ToLower() or ToUpper()
ToLower() and ToUpper() create new string objects on every call, which matters in loops or hot paths. They also have the "Turkish I" problem — in Turkish culture, 'I'.ToLower() produces 'ı' (dotless i), not 'i'.
String.Compare for Ordering
Common Pitfalls
- Using
ToLower()orToUpper()for comparison instead ofStringComparison: This allocates a new string on every comparison and fails with the Turkish I problem.string.Equals(a, b, StringComparison.OrdinalIgnoreCase)is faster and correct across all cultures. - Forgetting that
==is always case-sensitive: There is no way to make the==operator case-insensitive in C#. You must usestring.Equals()with aStringComparisonparameter or useStringComparerfor collections. - Using
InvariantCultureIgnoreCasewhenOrdinalIgnoreCasesuffices:InvariantCultureis slower because it applies linguistic rules. For non-linguistic comparisons (identifiers, paths, keys),OrdinalIgnoreCaseis both faster and more predictable. - Creating a Dictionary without
StringComparerand then doing case-insensitive lookups: Without passingStringComparer.OrdinalIgnoreCaseto the constructor, dictionary keys are case-sensitive. Manually callingToLower()on keys is error-prone and slower. - Assuming
Contains()without aStringComparisonparameter is case-insensitive: The parameterlessstring.Contains(string)is case-sensitive in .NET. The overload acceptingStringComparisonwas added in .NET Core 2.1. On .NET Framework, useIndexOfwithStringComparisoninstead.
Summary
- C# has no case-insensitive
==operator — usestring.Equals(a, b, StringComparison.OrdinalIgnoreCase) - Use
StringComparison.OrdinalIgnoreCasefor identifiers, paths, and configuration keys - Use
StringComparison.CurrentCultureIgnoreCasefor user-facing text comparison - Pass
StringComparer.OrdinalIgnoreCasetoDictionary,HashSet, and LINQ methods - Avoid
ToLower()/ToUpper()for comparisons — they allocate strings and have Unicode edge cases
Related reading
- Is there a data structure in C like a ConcurrentQueue which allows me to await an empty queue until an item is added?
- Is there a Date-only data type in C?
- Is there a difference between string.Empty vs null?
- Is there a difference between throw and throw ex?
- Is there a difference between using two where clauses or using in my LINQ query?
- Is there a framework for distributed job/workers in .net core
- Is there a “free” alternative to .NET Reflector?
- Is there a good radixsort-implementation for floats in C

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.