Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ToString() default CultureInfo

I think I understand the CultureInfo usage.

If I do simple :

const int a = 5;
string b = a.ToString();

is it equal to :

const int a = 5;
string b = a.ToString(CultureInfo.InvariantCulture);

In other words, does ToString() by default use InvariantCulture or CurrentCulture or neither ?

like image 247
mishap Avatar asked Apr 04 '13 18:04

mishap


People also ask

What does CultureInfo Invariantculture mean?

The invariant culture is culture-insensitive; it is associated with the English language but not with any country/region. You specify the invariant culture by name by using an empty string ("") in the call to a CultureInfo instantiation method. CultureInfo.

What is C# CultureInfo?

The CultureInfo class provides culture-specific information, such as the language, sublanguage, country/region, calendar, and conventions associated with a particular culture. This class also provides access to culture-specific instances of the DateTimeFormatInfo, NumberFormatInfo, CompareInfo, and TextInfo objects.


3 Answers

ToString will use CurrentCulture, not InvariantCulture if you do not specify a culture.

like image 165
Oded Avatar answered Oct 06 '22 23:10

Oded


ToString() uses CurrentCulture when not specified

See: http://msdn.microsoft.com/en-us/library/6t7dwaa5(v=vs.85).aspx

"The return value is formatted with the general numeric format specifier ("G") and the NumberFormatInfo for the current culture."

like image 33
Philippe Leybaert Avatar answered Oct 06 '22 23:10

Philippe Leybaert


The ToString implementation of all built-in classes and numeric types uses by default the CultureInfo.CurrentCulture culture, the culture used by the current thread.

This means that the current culture (and therefore your string formatting and parsing functions) will be different from one system to another. In my opinion this is a design mistake, and it has bitten people in the past. It should have defaulted to InvariantCulture and give the same results across systems, but unfortunately it doesn't.

like image 39
Daniel A.A. Pelsmaeker Avatar answered Oct 07 '22 00:10

Daniel A.A. Pelsmaeker