鉴于以下两种文化:
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”文化相同?
答案 0 :(得分:31)
是。
例如:InvariantCulture使用货币的国际符号:“¤”与美元符号:格式化货币时的“$”。
然而,在大多数情况下,它们非常相似。 修改:en-US
和Invariant
之间的差异列表:
en-US Invariant
===================== ================== ==================
Number 123456.78 +123456.78
Currency Symbol $ ¤
Currency $123456.78 ¤123456.78
Short Date 1/11/2012 01/11/2012
Time 10:36:52 PM 22:36:52
Metric No Yes
Long Date Wednesday, January 11, 2012 Wednesday, 11 January, 2012
Year Month January, 2012 2012 January
答案 1 :(得分:4)
存在一些实际差异(在Watch窗口中检查两个值),但最相关的区别是 intent 。 InvariantCulture显示您在独立于文化中解析某些数据的意图(如果与英语相关),而en-US声明您以美国特定方式解析数据的实际意图。
答案 2 :(得分: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以文本形式存储在数据库中的所有日期将继续正常工作......
答案 3 :(得分:1)
简短回答是的。 InvariantCulture就是它所说的,而不是特定的文化。它是英语,但不是特定区域
您可以在此处详细了解:MSDN
答案 4 :(得分:1)
考虑数据的意图非常重要。如果您要序列化,请确保使用InvariantCulture。
请参阅:http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.aspx
来自microsoft文档:
动态文化数据
除了不变的文化,文化数据是动态的。这是 即使是预定义的文化也是如此。 ...
注意
保存数据时,您的应用应使用不变文化, 使用二进制格式,或使用特定的与文化无关的格式。 根据与a关联的当前值保存数据 除了不变的文化之外,特定的文化可能会成为 如果文化发生变化,那么其意义不可读或可能会发生变化。
我刚刚遇到过这样的情况,用户将他的区域和语言设置设置为英语(美国),但是他选择了他的个人日期格式为dd-MMM-yy。他收到了一个客户的项目,其日期为默认的en-US格式“4/29/2010 1:45:30 PM”和代码:
customValue = DateTime.Parse(customValue.ToString(), CultureInfo.CreateSpecificCulture( “EN-US”));
引发了一个例外,因为他的本地偏好超越了典型的en-US格式。
答案 5 :(得分:0)
我知道他们有不同的CultureName
和LCID
(请参阅this list)。
此外,货币符号不同 - ¤表示InvariantCulture,$表示en-US。
It is used in almost any method in the Globalization namespace that requires a culture.
建议大多数情况下,它们是可以互换的。但是,这些名称会表达意图,因此在使用CultureInfo
时应该考虑这一点。