UpperCase when comparing strings
February 23rd, 2011
I today discovered something I wanted to share with you:
When normalizing strings, it is highly recommended that you use ToUpperInvariant instead of ToLowerInvariant because Microsoft has optimized the code for performing uppercase comparisons.
ToUpperInvariant is preferred because it makes all characters round-trip. See msdn.microsoft.com/en-us/library/bb386042.aspx. For comparisons, write
"a".Equals("A",StringComparison.OrdinalIgnoreCase)
In 99,9 percent of all cases it doesn’t matter that much.
I tried benchmarking ToUpperInvariant vs ToLowerInvariant. I cannot find any difference in their performance under .NET 2.0 or 3.5. Certainly not anything that warrant “highly recommending” using one over the other.





