.NET:InvariantCulture 和 en-US 之间有什么区别吗?

发布于 2024-08-23 05:15:41 字数 518 浏览 7 评论 0原文

考虑到以下两种文化:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

凝望流年 2024-08-30 05:15:41

是的。

例如: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.

北恋 2024-08-30 05:15:41

存在一些实际差异(在“监视”窗口中检查这两个值),但最相关的差异是意图。 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.

初熏 2024-08-30 05:15:41

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

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 以文本形式存储在数据库中的所有日期将继续有效......

Well, if you look at what your snippet of code might produce:

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());

You'll see some differences:

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

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...

祁梦 2024-08-30 05:15:41

考虑数据的意图非常重要。如果要序列化,请确保使用 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 格式。

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.

最终幸福 2024-08-30 05:15:41

简短的回答是的。 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

饮湿 2024-08-30 05:15:41

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

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

来自 InvariantCulture

它几乎用于全球化命名空间中需要区域性的任何方法。

表明在大多数情况下,它们是可以互换的。但是,这些名称确实表明了意图,因此您在使用 CultureInfo 时应该考虑这一点。

I know they have a different CultureName and LCID (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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文