19

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 ?

mishap
  • 8,176
  • 14
  • 61
  • 92

3 Answers3

28

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

Oded
  • 489,969
  • 99
  • 883
  • 1,009
5

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

Philippe Leybaert
  • 168,566
  • 31
  • 210
  • 223
4

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.

Daniel A.A. Pelsmaeker
  • 47,471
  • 20
  • 111
  • 157