mishap
mishap

Reputation: 8515

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 ?

Upvotes: 19

Views: 15066

Answers (3)

Philippe Leybaert
Philippe Leybaert

Reputation: 171774

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

Upvotes: 5

Daniel A.A. Pelsmaeker
Daniel A.A. Pelsmaeker

Reputation: 50326

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.

Upvotes: 4

Oded
Oded

Reputation: 499002

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

Upvotes: 28

Related Questions