3

I know that culture rules for dates/numbers is enough for an entire book, I think I have a simple question though..

Does using InvariantCulture basically mean that you explicitly define what culture the value (date/number/whatever) will be inputted/displayed as? And it overrides any other culture setting (such as the user agent's setting)?

If an app is built for an audience of one and only one culture, would it make sense to use InvariantCulture and define how you want values inputted/displayed each time?

notAnonymousAnymore
  • 2,637
  • 9
  • 49
  • 74
  • 2
    ["use the InvariantCulture to ensure that the behavior will be consistent regardless of the culture settings of the system"](http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.invariantculture(v=vs.71).aspx) – Sayse Jul 20 '13 at 15:20
  • 1
    It's a very confusing topic for me. Each MSDN doc I read confuses me more, hence "plain english". But thanks - I read – notAnonymousAnymore Jul 20 '13 at 15:22
  • 1
    InvariantCulture is the "plain english" culture. – Hans Passant Jul 20 '13 at 15:48
  • You could also see this: [what-is-the-invariant-culture](http://stackoverflow.com/questions/2423377/what-is-the-invariant-culture) – nawfal Mar 08 '14 at 14:54

3 Answers3

9

Does using InvariantCulture basically mean that you explicitly define what culture the value (date/number/whatever) will be inputted/displayed as?

No. It's just a culture which is a bit like US English, except for a few things like currency sign. It's usually used to format/consume text which is to be understood or was produced by another computer rather than a human.

If an app is built for an audience of one and only one culture, would it make sense to use InvariantCulture and define how you want values inputted/displayed each time?

No, you would use the relevant CultureInfo for that culture. You could also explicitly define the format for dates etc, but that's an orthogonal concern. It may well be best to use one of the predefined standard formats for that culture, but if none of those meet your needs you can always be explicit.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Ah ok.. So use the relevnt CultureInfo, and then explicitly override that when needed? Thanks for the answer.. – notAnonymousAnymore Jul 20 '13 at 15:24
  • @user982119: You don't override the culture - you explicitly specify the *format* where necessary. You understand that you can specify *both*, right? – Jon Skeet Jul 20 '13 at 16:54
  • Could you point me to an example? Of where specifying both, as you mean it, would be necessary/useful.. – notAnonymousAnymore Jul 20 '13 at 19:37
  • @user982119: Sure - say you wanted to specify "format the date with the day of the week, in this culture" you could use `dateTime.ToString("dddd dd MMMM yyyy", someCulture)` – Jon Skeet Jul 20 '13 at 19:47
6

InvariantCulture is Independent of any culture or any factor. For example if you're using new CultureInfo("en-US") it will get you US English Culture(Which may not be actual US English Culture because OS gives you option to change these setting in Control Panel) it will return the modified version of Culture of "en-US" if any custom formatting applied to it.

In other words InvariantCulture will always gives you a Culture which can never be changed across Systems.


Let's assume you want to serialize some value(say double) and pass to another application or some other thread which is running in different culture leads to serious problems.

Consider the following code

Thread.CurrentThread.CurrentCulture = new CultureInfo("fr");
double source = 455.62d;
string serialized = source.ToString();//455,62 since `, is the decimal seperator` in "fr"

Thread t = new Thread((x) =>
{
    double deserialized = double.Parse(((string)x));
    Console.WriteLine(string.Format("Deserialized value is {0}", deserialized));//outputs 45562 
});
t.CurrentCulture = new CultureInfo("en-US");
t.Start(serialized);

numbers matters more right? consider this string denotes AccountBalance?

Hope this helps

Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
5

With specific regard to DateTime, the best advice I can give is to use CultureInfo.InvariantCulture for ParseExact and ToString when you have an exact format in mind. (You can also use it with Parse if you happen to know that your input format matches the invariant culture's formats, but I typically avoid this.)

Don't use the invariant culture when interacting with a user.

Keep in mind that the culture contains several items, including the ordering of day/month/year parts, the date and time part separator characters, the character encoding, and the language-specific names of the days of the week, months of the year, and abbreviations including am/pm.

Some examples of when you should use the invariant culture:

  • Files on disk, especially text files of a predefined format
  • External APIs, especially JSON or XML
  • Unit tests, unless you are specifically testing culture issues

Some examples of when you should use a specific culture such as en-US:

  • User Interface
  • Reports
Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575