Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is difference between ToUpperInvariant() and ToUpper(new CultureInfo("en-US", false))

Tags:

c#

.net

I live in Turkey and I need to capitalize a key in my program. I had to use ToUpper(new CultureInfo("en-US", false)) instead of ToUpper() because this function behaves different in computers that are set to Turkish culture(See below picture). Then i saw there is also culture invariant upper function: ToUpperInvariant().

I searched but i could not find any information difference between these functions. Is there any difference at all?

enter image description here enter image description here

source:http://www.moserware.com/2008/02/does-your-code-pass-turkey-test.html

like image 921
onur demir Avatar asked Mar 20 '18 12:03

onur demir


1 Answers

ToUpper() is the same as ToUpper(CultureInfo.CurrentCulture), whereas ToUpperInvariant() is the same as ToUpper(CultureInfo.InvariantCulture), the comments hint that you already figured that out.

So of course there is a difference here - CultureInfo.InvariantCulture should only be used when not interacting with humans (parsers etc), as it gives a consistent result, whereas ToUpper(CultureInfo.CurrentCulture) varies quite a lot between computers, servers, etc.

CultureInfo.InvariantCulture is an english-inspired culture similar to but is not equal to en-US and is not bound to any country or region, and cannot be customized by users (as explicitly stated in the documentation).

As to explicitly answer your question regarding ToUpper - yes there are differences. In all of those cases (presented below), ToUpperInvariant() is the same char as the lowercase source:

lc    en-US     Invariant
==    =====     =========

µ     Μ         µ
ı     I         ı
ſ     S         ſ
Dž     DŽ         Dž
Lj     LJ         Lj
Nj     NJ         Nj
Dz     DZ         Dz
ͅ      Ι         ͅ  //  ͅͅͅͅͅͅͅthis one lives in the 4th dimension. 
ς     Σ         ς
ϐ     Β         ϐ
ϑ     Θ         ϑ
ϕ     Φ         ϕ
ϖ     Π         ϖ
ϰ     Κ         ϰ
ϱ     Ρ         ϱ
ϵ     Ε         ϵ
ẛ     Ṡ         ẛ
ι     Ι         ι
like image 99
Tewr Avatar answered Sep 29 '22 10:09

Tewr