advertise with us

String.Equals with OrdinalIgnoreCase compared to ToLower / ToUpper for string insensitive comparisons in C#

In C#, when checking if strings are equal in a case insensitive way prefer string.Equals with OrdinalIgnoreCase over using ToLower() / ToUpper().

Why?

Correctness
Using ToUpper or ToLower can lead to incorrect results in certain cultures and languages. Not all languages follow the simple upper-to-lowercase mapping that English does. For example, in Turkish, the lowercase of the uppercase letter “I” is “ı” (dotless i), not “i”.

Readability
Using string.Equals makes it clear that you’re performing a comparison. Methods like ToUpper or ToLower don’t obviously imply a comparison, so it can make your code harder to understand.

Performance
The string.Equals method is generally more efficient because it doesn’t need to create a new string instance as ToUpper or ToLower would. This can make a big difference when you’re comparing large strings or performing the operation many times.

Here’s some benchmarks I took on .NET 8 with BenchmarkDotNet which compare two simple strings ⬇ …

I’ve included string Compare in the benchmarks above just for comparative purposes but this is more suited to sorting strings so will be slower than string.Equals.


Need Help with Your C# Projects?

We offer expert support and development services for projects of any size. Contact us for a free consultation and see how we can help you succeed.

CONTACT US NOW