What is wrong with ToLowerInvariant()?

C#.Net.Net 4.0

C# Problem Overview


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?

C# Solutions


Solution 1 - C#

Google gives a hint pointing to CA1308: Normalize strings to uppercase

It says:

> Strings should be normalized to uppercase. A small group of characters, when they are converted to lowercase, cannot make a round trip. To make a round trip means to convert the characters from one locale to another locale that represents character data differently, and then to accurately retrieve the original characters from the converted characters.

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 ;)

Solution 2 - C#

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. 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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJL.View Question on Stackoverflow
Solution 1 - C#TomTomView Answer on Stackoverflow
Solution 2 - C#HenriView Answer on Stackoverflow