rulururu

post UpperCase when comparing strings

February 23rd, 2011

Filed under: .NET — Kai @ 11:42 am

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.

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

ruldrurd
Powered by WordPress, Content and Design by Kai Bellmann
Entries (RSS) and Comments (RSS)