Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is wrong with ToLowerInvariant()?

Tags:

c#

.net

.net-4.0

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?

like image 704
JL. Avatar asked May 10 '10 09:05

JL.


People also ask

What is the difference between ToLower and ToLowerInvariant?

ToLower() uses the default culture while String. ToLowerInvariant() uses the invariant culture.

What is ToLowerInvariant C#?

ToLowerInvariant() method in C# is used to return a copy of this String object converted to lowercase using the casing rules of the invariant culture.

What is to upper invariant?

The ToUpperInvariant method is equivalent to ToUpper(CultureInfo. InvariantCulture) . The method is recommended when a collection of strings must appear in a predictable order in a user interface control. This method does not modify the value of the current instance.


1 Answers

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

like image 134
TomTom Avatar answered Sep 23 '22 04:09

TomTom