32

鉴于以下两种文化:

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

我要检查两种文化特有的每一条信息,例如:

c1.DateTimeInfo.ShortDatePattern;
c2.DateTimeInfo.ShortDatePattern;

c1.DateTimeInfo.LongDatePattern;
c2.DateTimeInfo.LongDatePattern;

c1.NumberFormat.CurrencyDecimalDigits;
c2.NumberFormat.CurrencyDecimalDigits;

c1.TextInfo.IsRightToLeft;
c2.TextInfo.IsRightToLeft;

我会发现任何差异吗?

换句话说,InvariantCulture 在所有方面都与“en-US”文化相同吗?

4

6 回答 6

42

Yes.

For example: InvariantCulture uses the international symbol for currency: "¤" versus the dollar sign: "$" when formatting currency.

For the most part, however, they're very similar.

于 2010-02-24T20:39:14.860 回答
5

There are some actual differences (check both values in a Watch window), but the most relevant difference is the intent. InvariantCulture shows your intent of parsing some data in a culture independent, if english related, manner, whereas en-US declares your actual intent to parse data in a US specific manner.

于 2010-02-24T20:41:20.280 回答
3

好吧,如果你看看你的代码片段可能会产生什么:

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

Console.WriteLine( c1.DateTimeFormat.ShortDatePattern.ToString());
Console.WriteLine( c2.DateTimeFormat.ShortDatePattern.ToString());

Console.WriteLine( c1.DateTimeFormat.LongDatePattern.ToString());
Console.WriteLine( c2.DateTimeFormat.LongDatePattern.ToString());

Console.WriteLine( c1.NumberFormat.CurrencyDecimalDigits.ToString());
Console.WriteLine( c2.NumberFormat.CurrencyDecimalDigits.ToString());

Console.WriteLine( c1.TextInfo.IsRightToLeft.ToString());
Console.WriteLine( c2.TextInfo.IsRightToLeft.ToString());

您会看到一些差异:

MM/dd/yyyy
M/d/yyyy
dddd, dd MMMM yyyy
dddd, MMMM dd, yyyy
2
2
False
False

试想一下,当美国失去它的支柱并决定开始使用欧洲风格的日期或转向公制时(公制是魔鬼的工具!我的车有四十根杆子,这就是我喜欢它的方式!),InvariantCulture 可以冷静而平稳地保持原样。因此,您使用 InvariantCulture 以文本形式存储在数据库中的所有日期将继续正常工作......

于 2010-02-24T20:53:34.037 回答
2

考虑数据的意图非常重要。如果您正在序列化,请确保使用 InvariantCulture。

请参阅:http: //msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.aspx

从微软文档:

动态文化数据

除了不变的文化,文化数据是动态的。即使对于预定义的文化也是如此。...

警告

保存数据时,您的应用程序应使用不变的文化、使用二进制格式或使用特定的与文化无关的格式。根据与特定文化相关联的当前值保存的数据,而不是不变的文化,可能会变得不可读,或者如果该文化发生变化,其含义可能会发生变化。

我最近刚刚遇到这种情况,用户将他的区域和语言设置设置为英语(美国),但将他的个人日期格式选择为 dd-MMM-yy。他从客户那里收到了一个项目,其日期采用默认的 en-US 格式“4/29/2010 1:45:30 PM”,代码为:

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

抛出异常,因为他的本地偏好覆盖了典型的 en-US 格式。

于 2012-01-10T20:51:56.740 回答
1

Short answer yes. InvariantCulture is what it says, not a specific culture. It is english, but not a specific region

you can read more about it here : MSDN

于 2010-02-24T20:39:41.423 回答
0

我知道他们有不同的CultureNameLCID(请参阅此列表)。

此外,货币符号不同 - ¤ 代表 InvariantCulture,$ 代表 en-US。

来自InvariantCulture

It is used in almost any method in the Globalization namespace that requires a culture.

建议在大多数情况下,它们是可以互换的。但是,这些名称确实表明了意图,因此在使用CultureInfo.

于 2010-02-24T20:46:57.170 回答