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

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

问题描述

我有像这样的文字的字符串

var foo = "FooBar";

我要声明第二字符串名为,使这个等于我的第一个第一和第四个字符,所以我这样做,像这样:

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 在我的括号,所以这行结束了,像这样:

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.

这将事关你的时候,你要转换的输入值的(读取)的所为字符串存储到的DateTime 浮动十进制。它还将问题,如果你试图格式化上述数据类型为字符串的(写)的显示或储存。

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

  • <一个href="http://geekswithblogs.net/kobush/archive/2007/09/16/avoidcommonglobalizationerrors.aspx">CultureInfo.InvariantCulture示例
  • <一个href="http://stackoverflow.com/questions/2583362/how-to-convert-string-to-double-with-proper-cultureinfo">CultureInfo.InvariantCulture在计算器
  • <一个href="http://msdn.microsoft.com/en-us/library/4c5zdc6a%28v=vs.100%29.aspx">CultureInfo.InvariantCulture MSDN文章
  • <一个href="http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo%28v=vs.71%29.aspx">$p$pdefined CultureInfo的名称
  • CultureInfo.InvariantCulture Example
  • CultureInfo.InvariantCulture on StackOverflow
  • CultureInfo.InvariantCulture MSDN Article
  • Predefined CultureInfo Names

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

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