If you’re using Resharper, Roslynator or similar you’ll likely see string comparisons using ToLower() or ToUpper() flagged.
Why are these C# string compare methods flagged?
Well these string comparisons are not safe in all cultures. The canonical example is the ‘Turkish i problem‘ which relates to how in Turkish the dot is present on the top of the uppercase version of ‘i’ and there is no dot on the lowercase version of the ‘i’. This means using .ToLower() etc. may return different results on machines with different culture settings.
On their Best practices for comparing strings in .NET page Microsoft recommends using StringComparison.Ordinal or StringComparison.OrdinalIgnoreCase for comparisons as your safe default for culture-agnostic string matching.
Aside from giving us a way to do a safe string compare, StringComparison also expresses intent more clearly and does not need to create strings so there’s a potential performance boost too.
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