.NET: Are there any differences between InvariantCulture and en-US?

后端 未结 6 1158
感情败类
感情败类 2020-11-30 06:04

Given the following two cultures:

CultureInfo c1 = InvariantCulture;
CultureInfo c2 = new CultureInfo(\"en-US\");

and i were to examine eve

6条回答
  •  一个人的身影
    2020-11-30 06:12

    It is very important to consider the intent of the data. If you are serializing make sure to use InvariantCulture.

    See: http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.aspx

    From the microsoft documentation:

    Dynamic Culture Data

    Except for the invariant culture, culture data is dynamic. This is true even for the predefined cultures. ...

    Caution

    When saving data, your application should use the invariant culture, use a binary format, or use a specific culture-independent format. Data saved according to the current values associated with a particular culture, other than the invariant culture, might become unreadable or might change in meaning if that culture changes.

    I just encountered this recently where the user had his Region and Language settings set to English (United States), but had chosen his personal date format to dd-MMM-yy. He received a project from a client with a date in the default en-US format "4/29/2010 1:45:30 PM" and the code:

    customValue = DateTime.Parse(customValue.ToString(), CultureInfo.CreateSpecificCulture("en-US"));

    threw an exception because his local preferences override what the typical en-US format.

提交回复
热议问题