CultureInfo.InvariantCulture是什么意思? [英] What does CultureInfo.InvariantCulture mean?

查看:155
本文介绍了CultureInfo.InvariantCulture是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一串这样的文本:

var foo = "FooBar";

我要声明第二个字符串,称为 bar 并使其等于我的第一个 foo 的第一个和第四个字符,所以我这样做是这样的:

I want to declare a second string called bar and make this equal to first and fourth character of my first foo, so I do this like so:

var bar = foo[0].ToString() + foo[3].ToString();

这可以按预期工作,但是 ReSharper 建议我将 Culture.InvariantCulture 放在方括号内,因此此行的结尾如下: p>

This works as expected, but ReSharper is advising me to put Culture.InvariantCulture inside my brackets, so this line ends up like so:

var bar = foo[0].ToString(CultureInfo.InvariantCulture)
        + foo[3].ToString(CultureInfo.InvariantCulture);

这是什么意思,会影响我的程序运行吗?

What does this mean, and will it affect how my program runs?

推荐答案

并非所有区域性都对日期和十进制/货币值使用相同的格式。

Not all cultures use the same format for dates and decimal / currency values.

当您将存储为字符串的输入值(read)转换为 DateTime float 双倍十进制。如果尝试将上述数据类型格式化为字符串(写入)以进行显示或存储,也将很重要。

This will matter for you when you are converting input values (read) that are stored as strings to DateTime, float, double or decimal. It will also matter if you try to format the aforementioned data types to strings (write) for display or storage.

如果您知道具体的根据您的日期和十进制/货币值会提前出现的文化,您可以使用特定的 CultureInfo 属性(即 CultureInfo( en-GB ))。例如,如果您期望用户输入。

If you know what specific culture that your dates and decimal / currency values will be in ahead of time, you can use that specific CultureInfo property (i.e. CultureInfo("en-GB")). For example if you expect a user input.

如果您愿意,则使用 CultureInfo.InvariantCulture 属性格式化或解析一个应该由独立于用户本地设置的软件可解析的字符串。

The CultureInfo.InvariantCulture property is used if you are formatting or parsing a string that should be parseable by a piece of software independent of the user's local settings.

默认值为 CultureInfo.InstalledUICulture ,因此默认的CultureInfo取决于正在执行的操作系统的设置。这就是为什么您始终要确保文化信息符合您的意图的原因(有关良好指南,请参见马丁的回答)。

The default value is CultureInfo.InstalledUICulture so the default CultureInfo is depending on the executing OS's settings. This is why you should always make sure the culture info fits your intention (see Martin's answer for a good guideline).

  • CultureInfo.InvariantCulture Example
  • CultureInfo.InvariantCulture on StackOverflow
  • CultureInfo.InvariantCulture MSDN Article
  • Predefined CultureInfo Names

这篇关于CultureInfo.InvariantCulture是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆