11

I am currently fixing FxCop issues so I encountered issue where I have to provide cultureinfo when converting a string using ToString() .

Currently in my code nothing we are passing as IFormatProvider so I have read some msdn articles saying that when you don't pass any value for cultureinfo it will assign a default value and when you specify CultureInfo as InvariantCulture it will be independent of any culture.

My question is, "Are default and CultureInfo.InvariantCulture one and the same? Can I replace all my code from default to InvariantCulture?"

Ex :

 int st = 123;
 String s = st.ToString(123); // this will be taken as default 
 String s = st.ToString(123, CultureInfo.InvariantCulture); // culture is specified externally 

Are the second and third lines equivalent?

Tom Zych
  • 13,329
  • 9
  • 36
  • 53
user3766691
  • 143
  • 1
  • 3
  • 7
  • _so is line 2nd and 3rd are same_ In your st value, yes. But they might be different on some cases.. – Soner Gönül Jun 23 '14 at 08:52
  • Which one is correct for you? `float st = 123.5f; String s1 = st.ToString( CultureInfo.GetCultureInfo("de-DE")); String s2 = st.ToString( CultureInfo.InvariantCulture); ` – L.B Jun 23 '14 at 08:54
  • Do not use InvariantCulture if you live in Europe and want to parse American dates. It will throw a wobbly. – tom redfern Jan 18 '19 at 17:50

2 Answers2

24

Is default and CultureInfo.InvariantCulture are one and the same?

No, absolutely not. The default culture depends (initially) on the operating system settings. The invariant culture is meant to be a "neutral" culture.

Your example of 123 isn't a great one, because most (all?) cultures will represent integers the same way - at least until you get into formats with grouping separators etc. (I don't think .NET supports non-Arabic numerals when formatting integers.)

Compare that with formatting a decimal value, for example:

decimal x = 123.45m;
Console.WriteLine(x.ToString()); // Might be 123,45
Console.WriteLine(x.ToString(CultureInfo.InvariantCulture)); // Always 123.45

If you run the above code in (say) France, the default culture will be French, which uses a comma as a decimal separator - so it will print out "123,45".

The rule of thumb to remember is that the invariant culture is suitable for machine-to-machine communications (e.g. formatting values in JSON or XML) whereas other cultures are more suitable for displaying information directly to users.

Although the default culture is originally based on the operating system settings, it can be changed using Thread.CurrentCulture and Thread.CurrentUICulture; the latter is used for looking up translated resources, whereas the former is used for formatting decisions like the above. You can set these properties on any thread, but typically you'd use Thread.CurrentThread.CurrentCulture = ...

Gianluca Ghettini
  • 11,129
  • 19
  • 93
  • 159
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Thanks Jon so in my case how do I fix FxCop violation what should I pass for format specifier ? – user3766691 Jun 23 '14 at 09:50
  • 2
    @user3766691: I can't possibly answer that without knowing what you're trying to do with your string. Look at the "rule of thumb" paragraph in my answer. – Jon Skeet Jun 23 '14 at 09:50
  • Currently am fixing FxCop violations in my code and wherever in code i am using .ToSring() method am getting this error so how do i fix these ? – user3766691 Jun 23 '14 at 10:29
  • What is the message you get? You shouldn't just replace all occurrences before knowing what the code does. – Patrick Hofman Jun 23 '14 at 10:33
  • 1
    @user3766691: You think about what you're trying to achieve in each situation. There's no one-size-fits-all answer here. – Jon Skeet Jun 23 '14 at 10:33
  • In Some line they are simply using the string value to show error messages and logging purposes .... so it is better for me to keep it remain same... – user3766691 Jun 23 '14 at 10:37
  • @user3766691: Logging (developer-facing) and error messages (user-facing) are quite possibly very different. I would probably err on the side of the invariant culture (or possibly a fixed *other* culture, e.g. US English) for logging, but for the error messages that may be very different. You need to understand the different contexts in which the strings will be read. – Jon Skeet Jun 23 '14 at 10:39
  • _because most cultures will represent integers the same way_ Jon, on what culture represents `123` other than `123`? I try to get all cultures with `CultureInfo.GetCultures` which takes parameter as `CultureTypes.AllCultures | CultureTypes.ReplacementCultures | CultureTypes.UserCustomCulture` but all returns `123` as a result. – Soner Gönül Jun 23 '14 at 10:51
  • 2
    @SonerGönül: I was hedging a little - in reality, some cultures don't use Arabic numerals, but the .NET CultureInfo still does... except for certain situations. (For example, if you format a month number in the Hebrew calendar system in an Israeli culture, it will use Israeli digits - but just 123.ToString(...) will still use Arabic digits. – Jon Skeet Jun 23 '14 at 10:55
3

No, they are not the same.

The first will take the regional settings from the computer or the culture settings from the application thread running.

The second one will take the English language, according to MSDN:

The invariant culture is culture-insensitive; it is associated with the English language but not with any country/region.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325