.net CultureInfo.InvariantCulture 是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9760237/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does CultureInfo.InvariantCulture mean?
提问by JMK
I have a string of text like so:
我有一串像这样的文本:
var foo = "FooBar";
I want to declare a second string called barand make this equal to first and fourth character of my first foo, so I do this like so:
我想声明第二个字符串,bar并使其等于我的第一个和第四个字符foo,所以我这样做:
var bar = foo[0].ToString() + foo[3].ToString();
This works as expected, but ReSharperis advising me to put Culture.InvariantCultureinside my brackets, so this line ends up like so:
这按预期工作,但ReSharper建议我将其放在Culture.InvariantCulture括号内,因此该行最终如下所示:
var bar = foo[0].ToString(CultureInfo.InvariantCulture)
+ foo[3].ToString(CultureInfo.InvariantCulture);
What does this mean, and will it affect how my program runs?
这是什么意思,它会影响我的程序的运行方式吗?
采纳答案by JohnB
Not all cultures use the same format for dates and decimal / currency values.
并非所有文化都对日期和十进制/货币值使用相同的格式。
This will matter for you when you are converting input values (read)that are stored as strings to DateTime, float, doubleor decimal. It will also matter if you try to format the aforementioned data types to strings (write)for display or storage.
当你转换输入值这个问题将你(读)存储为字符串 DateTime,float,double或decimal。如果您尝试将上述数据类型格式化为字符串(写入)以进行显示或存储,这也很重要。
If you know what specific culture that your dates and decimal / currency values will be in ahead of time, you can use that specific CultureInfoproperty (i.e. CultureInfo("en-GB")). For example if you expect a user input.
如果您提前知道日期和十进制/货币值将采用哪种特定文化,则可以使用该特定CultureInfo属性(即CultureInfo("en-GB"))。例如,如果您期望用户输入。
The CultureInfo.InvariantCultureproperty 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.InvariantCulture如果您正在格式化或解析一个应该可由独立于用户本地设置的软件解析的字符串,则使用该属性。
The default value is CultureInfo.InstalledUICultureso 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 answerfor a good guideline).
默认值是CultureInfo.InstalledUICulture默认的 CultureInfo 取决于正在执行的操作系统的设置。这就是为什么您应该始终确保文化信息符合您的意图(请参阅Martin 的回答以获得良好的指导)。
回答by Martin Liversage
When numbers, dates and times are formatted into strings or parsed from strings a culture is used to determine how it is done. E.g. in the dominant en-USculture you have these string representations:
当数字、日期和时间被格式化为字符串或从字符串解析时,文化被用来确定它是如何完成的。例如,在主流en-US文化中,您有这些字符串表示:
- 1,000,000.00 - one million with a two digit fraction
- 1/29/2013 - date of this posting
- 1,000,000.00 - 带两位数的一百万
- 2013 年 1 月 29 日 - 发布日期
In my culture (da-DK) the values have this string representation:
在我的文化 ( da-DK) 中,这些值具有以下字符串表示形式:
- 1.000.000,00 - one million with a two digit fraction
- 29-01-2013 - date of this posting
- 1.000.000,00 - 带两位数的一百万
- 29-01-2013 - 发帖日期
In the Windows operating system the user may even customize how numbers and date/times are formatted and may also choose another culture than the culture of his operating system. The formatting used is the choice of the user which is how it should be.
在 Windows 操作系统中,用户甚至可以自定义数字和日期/时间的格式,也可以选择不同于其操作系统文化的另一种文化。使用的格式是用户的选择,它应该是怎样的。
So when you format a value to be displayed to the user using for instance ToStringor String.Formator parsed from a string using DateTime.Parseor Decimal.Parsethe default is to use the CultureInfo.CurrentCulture. This allows the user to control the formatting.
因此,当您格式化要显示给用户的值时,例如使用ToStringorString.Format或从字符串解析使用DateTime.Parse或Decimal.Parse默认使用CultureInfo.CurrentCulture. 这允许用户控制格式。
However, a lot of string formatting and parsing is actually not strings exchanged between the application and the user but between the application and some data format (e.g. an XML or CSV file). In that case you don't want to use CultureInfo.CurrentCulturebecause if formatting and parsing is done with different cultures it can break. In that case you want to use CultureInfo.InvariantCulture(which is based on the en-USculture). This ensures that the values can roundtrip without problems.
然而,很多字符串格式化和解析实际上不是在应用程序和用户之间交换字符串,而是在应用程序和某些数据格式(例如 XML 或 CSV 文件)之间交换。在这种情况下,您不想使用,CultureInfo.CurrentCulture因为如果格式化和解析是用不同的文化完成的,它可能会中断。在这种情况下,您要使用CultureInfo.InvariantCulture(基于en-US文化)。这确保了这些值可以毫无问题地往返。
The reason that ReSharper gives you the warning is that some application writers are unaware of this distinction which may lead to unintended results but they never discover this because their CultureInfo.CurrentCultureis en-USwhich has the same behavior as CultureInfo.InvariantCulture. However, as soon as the application is used in another culture where there is a chance of using one culture for formatting and another for parsing the application may break.
ReSharper 向您发出警告的原因是一些应用程序编写者不知道这种区别,这可能会导致意外结果,但他们从未发现这一点,因为他们的CultureInfo.CurrentCultureisen-US与CultureInfo.InvariantCulture. 但是,一旦应用程序在另一种文化中使用,其中有可能使用一种文化进行格式化并使用另一种文化来解析应用程序,则应用程序可能会中断。
So to sum it up:
所以总结一下:
- Use
CultureInfo.CurrentCulture(the default) if you are formatting or parsing a user string. - Use
CultureInfo.InvariantCultureif you are formatting or parsing a string that should be parseable by a piece of software. - Rarely use a specific national culture because the user is unable to control how formatting and parsing is done.
CultureInfo.CurrentCulture如果您正在格式化或解析用户字符串,请使用(默认)。CultureInfo.InvariantCulture如果您正在格式化或解析应该可由软件解析的字符串,请使用。- 很少使用特定的民族文化,因为用户无法控制格式化和解析的方式。
回答by happybits
According to Microsoft:
根据微软的说法:
The CultureInfo.InvariantCulture property is neither a neutral nor a specific culture. It is a third type of culture that is culture-insensitive. It is associated with the English language but not with a country or region.
CultureInfo.InvariantCulture 属性既不是中立的也不是特定的文化。这是第三种文化不敏感的文化。它与英语相关,但与国家或地区无关。
(from http://msdn.microsoft.com/en-us/library/4c5zdc6a(vs.71).aspx)
(来自http://msdn.microsoft.com/en-us/library/4c5zdc6a(vs.71).aspx)
So InvariantCulture is similair to culture "en-US" but not exactly the same. If you write:
因此,InvariantCulture 类似于“en-US”文化,但并不完全相同。如果你写:
var d = DateTime.Now;
var s1 = d.ToString(CultureInfo.InvariantCulture); // "05/21/2014 22:09:28"
var s2 = d.ToString(new CultureInfo("en-US")); // "5/21/2014 10:09:28 PM"
then s1 and s2 will have similair format but InvariantCulture adds leading zeroes and "en-US" uses AM or PM.
然后 s1 和 s2 将具有类似格式,但 InvariantCulture 添加前导零,“en-US”使用 AM 或 PM。
So InvariantCulture is better for internal usage, when you e.g saves a date to a text-file or parses data. And a specified CultureInfo is better when you present data (date, currency...) to the end user.
因此 InvariantCulture 更适合内部使用,例如,当您将日期保存到文本文件或解析数据时。当您向最终用户提供数据(日期、货币...)时,指定的 CultureInfo 会更好。
回答by Turing Machine
For things like numbers (decimal points, commas in amounts), they are usually preferred in the specific culture.
对于数字(小数点、逗号)之类的东西,它们通常在特定文化中是首选。
A appropriate way to do this would be set it at the culture level (for German) like this:
一个合适的方法是将它设置在文化级别(德语),如下所示:
Thread.CurrentThread.CurrentCulture.NumberFormat = new CultureInfo("de").NumberFormat;
回答by Neil Thompson
JetBrains offer a reasonable explanation,
JetBrains 提供了合理的解释,
"Ad-hoc conversion of data structures to text is largely dependent on the current culture, and may lead to unintended results when the code is executed on a machine whose locale differs from that of the original developer. To prevent ambiguities, ReSharper warns you of any instances in code where such a problem may occur."
“将数据结构临时转换为文本在很大程度上取决于当前的文化,并且当代码在与原始开发人员的语言环境不同的机器上执行时,可能会导致意想不到的结果。为了防止歧义,ReSharper 警告您代码中可能出现此类问题的任何实例。”
but if I am working on a site I know will be in English only, I just ignore the suggestion.
但如果我在一个我知道只有英文的网站上工作,我就会忽略这个建议。

