.net 什么是不变文化?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2423377/
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 is the Invariant Culture?
提问by Ricky
Could anybody give an example to demonstrate the usage of the Invariant Culture? I don't understand what the documentation describes.
有人可以举一个例子来演示不变文化的用法吗?我不明白文档描述了什么。
采纳答案by Andrew Bezzub
The invariant culture is a special culture which is useful because it will not change. The current culture can change from one user to another, or even from one run to another, so you can't rely on it staying the same.
不变文化是一种特殊的文化,它是有用的,因为它不会改变。当前的文化可以从一个用户变为另一个用户,甚至从一个用户运行到另一个用户,因此您不能指望它保持不变。
Being able to use the same culture each time is very important in several flows, for example, serialization: you can have 1,1 value in one culture and 1.1 in another. If you will try to parse "1,1" value in the second culture, then parsing will fail. However you can use the invariant culture to convert a number to a string and later parse it back from any computer with any culture set.
每次能够使用相同的文化在多个流程中非常重要,例如,序列化:您可以在一种文化中拥有 1,1 值,而在另一种文化中拥有 1.1 值。如果您尝试解析第二种文化中的“1,1”值,则解析将失败。但是,您可以使用不变区域性将数字转换为字符串,然后从具有任何区域性集的任何计算机将其解析回来。
// Use some non-invariant culture.
CultureInfo nonInvariantCulture = new CultureInfo("en-US");
Thread.CurrentThread.CurrentCulture = nonInvariantCulture;
decimal dec = 1.1m;
string convertedToString = dec.ToString();
// Simulate another culture being used,
// following code can run on another computer.
nonInvariantCulture.NumberFormat.NumberDecimalSeparator = ",";
decimal parsedDec;
try
{
// This fails because value cannot be parsed.
parsedDec = decimal.Parse(convertedToString);
}
catch (FormatException)
{
}
// However you always can use Invariant culture:
convertedToString = dec.ToString(CultureInfo.InvariantCulture);
// This will always work because you serialized with the same culture.
parsedDec = decimal.Parse(convertedToString, CultureInfo.InvariantCulture);
回答by TomTom
A fake culture based on English with defined behavior. Great to write out, for example, stuff into config files so it can be read and written regardless of the culture the user has defined.
一种基于英语的虚假文化,具有明确的行为。例如,非常适合将内容写入配置文件,以便无论用户定义的文化如何都可以读取和写入。
Basically it is a specific culture that is artificial and will not change.
基本上它是一种特定的文化,是人为的,不会改变。
回答by Gishu
It is used for stuff that is the same regardless of culture (that doesn't need to be translated to some culture X to be appropriate)
它用于无论文化如何都相同的东西(不需要翻译成某种文化 X 才合适)
as for an example - https://msdn.microsoft.com/en-us/library/4c5zdc6a(v=vs.100).aspx. When you write out an app-specific file which the user shouldn't be messing around with, you should use InvariantCulture for all methods that take in a culture parameter.
例如 - https://msdn.microsoft.com/en-us/library/4c5zdc6a(v=vs.100).aspx。当您写出用户不应弄乱的特定于应用程序的文件时,您应该将 InvariantCulture 用于所有接受文化参数的方法。
Note that per the docs linked above:
请注意,根据上面链接的文档:
However, an application should use the invariant culture only for processes that require culture-independent results, such as formatting and parsing data that is persisted to a file.
但是,应用程序应该仅将不变区域性用于需要与区域性无关的结果的进程,例如格式化和解析持久化到文件的数据。
回答by CharithJ
The invariant culture is culture-insensitive; it is associated with the English language but not with any country/region.
不变文化是文化不敏感的;它与英语相关,但与任何国家/地区无关。
CultureInfo.InvariantCulture retrieves an instance of the invariant culture. It can be used in almost any method in the System.Globalization namespace that requires a culture.
CultureInfo.InvariantCulture 检索固定区域性的实例。它几乎可以用于 System.Globalization 命名空间中需要区域性的任何方法。
The objects returned by properties such as CompareInfo, DateTimeFormat, and NumberFormat also reflect the string comparison and formatting conventions of the invariant culture. The InvariantCulture property comes handy when you want to display persist datain a culture-independentformat.
CompareInfo、DateTimeFormat 和 NumberFormat 等属性返回的对象也反映了不变区域性的字符串比较和格式约定。当您想要以与文化无关的格式显示持久数据时,InvariantCulture 属性会派上用场。
For instance, if you want to display a number or datetime in a specific format independent of the application's current culture you can use CultureInfo.InvariantCulture.
例如,如果您想以独立于应用程序当前文化的特定格式显示数字或日期时间,您可以使用CultureInfo.InvariantCulture。
回答by Shadi Namrouti
It is a universal simple non-regional-specific English language and other related info. It's like the language of the programming language itself. You can rely on it in setting up a universal calendar; in situation where you need to generate controller names, URL's, delegate names ...etc. and need things to act naturally and universally among all users.
它是一种通用的简单的非区域特定的英语语言和其他相关信息。这就像编程语言本身的语言。您可以依靠它来设置通用日历;在需要生成控制器名称、URL、委托名称等的情况下。并且需要在所有用户之间自然而普遍地发挥作用。

