ToString() default CultureInfo
Asked Answered
C

3

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 ?

Courtund answered 4/4, 2013 at 18:42 Comment(0)
I
28

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

Intersexual answered 4/4, 2013 at 18:43 Comment(0)
I
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."

Imparadise answered 4/4, 2013 at 18:43 Comment(0)
M
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.

Mark answered 4/4, 2013 at 18:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.