tags:

views:

411

answers:

2

I have the following line of code:

var connectionString = configItems.Find(item => item.Name.ToLowerInvariant() == "connectionstring");

VS 2010 Code analysis is telling me the following:

Warning 7 CA1308 : Microsoft.Globalization : In method ... replace the call to 'string.ToLowerInvariant()' with String.ToUpperInvariant().

Does this mean ToUpperInvariant() is more reliable?

+11  A: 

Google gives a hint pointing to http://msdn.microsoft.com/en-us/library/bb386042.aspx

It says:

Strings should be normalized to uppercase. There is a small group of characters that when converted to lowercase cannot make a round trip. To make a round trip means to send the characters from one locale to another one that represents of character data differently, and retrieve it without loss.

So, yes - ToUpper is more reliable than ToLower.

In the future I suggest googling first - I do that for all those FxCop warnings I get thrown around ;) Helps a lot to read the corresponding documentation ;)

TomTom
+1 for "Helps a lot to read the corresponding documentation" (and also for being absolutely correct...)
gehho
+2  A: 

Besides what TomTom says, .net is optimized for string comparison in upper case. So using upper invariant is theoretically faster than lowerinvariant.

This is indeed stated in CLR via C# as pointed out in the comments. The following link quotes that part of the book. Im not sure if this is of course really true since there is nothing to be found on MSDN about this topic. The string comparison guide on msdn mentions that toupperinvariant and tolowerinvariant are equal and does not prefer the former.

http://theiterator.com/2009/01/string-comparisons-in-c/

Henri
+1 true. I actually read about that some days ago and was quite surprised that there is a difference. However, I think the difference should be pretty small.
gehho
Any reference to that? Doing .NET 10 years and being qconsidered verygood - I did not know that ;) Would love to have some reference.
TomTom
I think I saw that in CLR via C# (J Richter). Still prefer to use an explicit StringComparer though.
Phil