What\'s the difference and when to use what? What\'s the risk if I always use ToLower()
and what\'s the risk if I always use ToLowerInvariant()
?
String.ToLower()
uses the default culture while String.ToLowerInvariant()
uses the invariant culture. So you are essentially asking the differences between invariant culture and ordinal string comparision.
I think this can be useful:
http://msdn.microsoft.com/en-us/library/system.string.tolowerinvariant.aspx
update
If your application depends on the case of a string changing in a predictable way that is unaffected by the current culture, use the ToLowerInvariant method. The ToLowerInvariant method is equivalent to ToLower(CultureInfo.InvariantCulture). The method is recommended when a collection of strings must appear in a predictable order in a user interface control.
also
...ToLower is very similar in most places to ToLowerInvariant. The documents indicate that these methods will only change behavior with Turkish cultures. Also, on Windows systems, the file system is case-insensitive, which further limits its use...
http://www.dotnetperls.com/tolowerinvariant-toupperinvariant
hth
Depending on the current culture, ToLower might produce a culture specific lowercase letter, that you aren't expecting. Such as producing ınfo
without the dot on the i instead of info
and thus mucking up string comparisons. For that reason, ToLowerInvariant should be used on any non-language-specific data. When you might have user input that might be in their native language/character-set, would generally be the only time you use ToLower.
See this question for an example of this issue: C#- ToLower() is sometimes removing dot from the letter "I"
TL;DR:
When working with "content" (e.g. articles, posts, comments, names, places, etc.) use ToLower()
. When working with "literals" (e.g. command line arguments, custom grammars, strings that should be enums, etc.) use ToLowerInvariant()
.
Examples:
=Using ToLowerInvariant
incorrectly=
In Turkish, DIŞ
means "outside" and diş
means "tooth". The proper lower casing of DIŞ
is dış
. So, if you use ToLowerInvariant
incorrectly you may have typos in Turkey.
=Using ToLower
incorrectly=
Now pretend you are writing an SQL parser. Somewhere you will have code that looks like:
if(operator.ToLower() == "like")
{
// Handle an SQL LIKE operator
}
The SQL grammar does not change when you change cultures. A Frenchman does not write SÉLECTIONNEZ x DE books
instead of SELECT X FROM books
. However, in order for the above code to work, a Turkish person would need to write SELECT x FROM books WHERE Author LİKE '%Adams%'
(note the dot above the capital i, almost impossible to see). This would be quite frustrating for your Turkish user.