.NET:InvariantCulture 和 en-US 之间有什么区别吗?
考虑到以下两种文化:
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”文化相同?
Given the following two cultures:
CultureInfo c1 = InvariantCulture;
CultureInfo c2 = new CultureInfo("en-US");
and i were to examine every piece of information specific to both cultures, e.g.:
c1.DateTimeInfo.ShortDatePattern;
c2.DateTimeInfo.ShortDatePattern;
c1.DateTimeInfo.LongDatePattern;
c2.DateTimeInfo.LongDatePattern;
c1.NumberFormat.CurrencyDecimalDigits;
c2.NumberFormat.CurrencyDecimalDigits;
c1.TextInfo.IsRightToLeft;
c2.TextInfo.IsRightToLeft;
Would i find any differences?
In other words, is the InvariantCulture, for all purposes, identical to the "en-US" culture?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
是的。
例如:InvariantCulture 在格式化货币时使用国际货币符号:“¤”与美元符号:“$”。
然而,在大多数情况下,它们非常相似。
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.
存在一些实际差异(在“监视”窗口中检查这两个值),但最相关的差异是意图。 InvariantCulture 显示您以独立于文化(如果与英语相关)的方式解析某些数据的意图,而 en-US 则声明您以美国特定方式解析数据的实际意图。
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.
好吧,如果你看看你的代码片段可能会产生什么:
你会看到一些差异:
想一想,当美国失去它的支柱并决定开始使用欧洲风格的日期或转向公制系统时(公制系统是魔鬼的工具!我的车能开四十杆,这就是我喜欢的方式!),InvariantCulture 可以冷静而顺利地保持原样。因此,您使用 InvariantCulture 以文本形式存储在数据库中的所有日期将继续有效......
Well, if you look at what your snippet of code might produce:
You'll see some differences:
And just think, when the US loses it's backbone and decides to start using European style dates or moves to the metric system (the metric system is the tool of the devil! My car gets forty rods to the hogshead and that's the way I likes it!), the InvariantCulture can just coolly and smoothly stay the way it is. So all those dates you've stashed away in a database in text form using the InvariantCulture will continue to just work...
考虑数据的意图非常重要。如果要序列化,请确保使用 InvariantCulture。
请参阅:http://msdn.microsoft.com/en-us /library/system.globalization.cultureinfo.aspx
来自微软文档:
我最近刚刚遇到这种情况,用户将其区域和语言设置设置为英语(美国),但将其个人日期格式选择为 dd-MMM-yy。他收到了客户发来的一个项目,其日期采用默认的 en-US 格式“4/29/2010 1:45:30 PM”,代码如下:
抛出异常,因为他的本地偏好覆盖了典型的 en-US 格式。
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:
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:
threw an exception because his local preferences override what the typical en-US format.
简短的回答是的。 InvariantCulture 顾名思义,不是一种特定的文化。它是英文的,但不是特定区域的,
您可以在这里阅读更多相关信息:MSDN
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
我知道他们有不同的
CultureName
和LCID
(请参阅 此列表)。此外,货币符号也不同 - ¤ 代表 InvariantCulture,$ 代表 en-US。
来自
InvariantCulture
:它几乎用于全球化命名空间中需要区域性的任何方法。
表明在大多数情况下,它们是可以互换的。但是,这些名称确实表明了意图,因此您在使用
CultureInfo
时应该考虑这一点。I know they have a different
CultureName
andLCID
(see this list).Additionally, the currency symbols are different - ¤ for InvariantCulture and $ for en-US.
From
InvariantCulture
:It is used in almost any method in the Globalization namespace that requires a culture.
Suggesting that for the most part, they are interchangeable. However, the names do state intent, so you should think about that when using
CultureInfo
.