C# 在 String.Format() 中真的需要 CultureInfo.CurrentCulture 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/475383/
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
Is CultureInfo.CurrentCulture really necessary in String.Format()?
提问by abatishchev
How do you think is really necessary to provide IFormatProvider
in method String.Format(string, object)
?
您认为如何IFormatProvider
在方法中真正有必要提供String.Format(string, object)
?
Is it better to write full variant
写完整的变体更好吗
String.Format(CultureInfo.CurrentCulture, "String is {0}", str);
or just
要不就
String.Format("String is {0}", str);
?
?
采纳答案by Joe
In general, you will want to use InvariantCulture if the string you are generating is to be persisted in a way that is independent of the current user's culture (e.g. in the registry, or in a file).
通常,如果要以独立于当前用户文化的方式(例如,在注册表中或文件中)保留您生成的字符串,您将需要使用 InvariantCulture。
You will want to use CurrentCulture for strings that are to be presented in the UI to the current user (forms, reports).
您将希望将 CurrentCulture 用于将在 UI 中呈现给当前用户(表单、报告)的字符串。
Subtle bugs can arise if you use CurrentCulture where you should be using InvariantCulture: bugs that only come to light when you have multiple users with different cultures accessing the same registry entry or file, or if a user changes his default culture.
如果您在应该使用 InvariantCulture 的地方使用 CurrentCulture,则可能会出现细微的错误:只有当您有多个不同文化的用户访问相同的注册表项或文件时,或者如果用户更改了他的默认文化时,这些错误才会出现。
Explicitly specifying CurrentCulture (the default if the IFormatProvider argument is omitted), is essentially documentation that demonstrates that you have considered the above and that the string being generated should use the current user's culture. That's why FxCop recommends that you should specify the IFormatProvider argument.
显式指定 CurrentCulture(如果省略 IFormatProvider 参数,则为默认值)本质上是证明您已考虑上述内容并且生成的字符串应使用当前用户的区域性的文档。这就是 FxCop 建议您应该指定 IFormatProvider 参数的原因。
回答by Ian P
It is especially useful if you care about localization (Globalization) in your application. That is, if you want your app to support multiple languages and culture specific formats, then you should use that.
如果您关心应用程序中的本地化(全球化),它尤其有用。也就是说,如果您希望您的应用支持多种语言和特定于文化的格式,那么您应该使用它。
回答by Spikolynn
No, you do not need to specify the culture unless your string contains culture specific elements such as decimal separators, currency, etc., which have to be rendered depending on the culture.
不,您不需要指定文化,除非您的字符串包含特定于文化的元素,例如小数点分隔符、货币等,这些元素必须根据文化进行呈现。
回答by Jeffrey Hantin
If you do not specify the IFormatProvider
(or equivalently pass null
) most argument types will eventually fall through to being formatted according to CultureInfo.CurrentCulture
. Where it gets interesting is that you can specify a custom IFormatProvider
that can get first crack at formatting the arguments, or override the formatting culture depending on other context.
如果您不指定IFormatProvider
(或等效地传递null
)大多数参数类型将最终落入根据CultureInfo.CurrentCulture
. 有趣的是,您可以指定一个自定义IFormatProvider
,该自定义可以首先破解参数的格式,或者根据其他上下文覆盖格式文化。
Note that CultureInfo.CurrentCulture
affects argument formatting, not resource selection; resource selection is controlled by CultureInfo.CurrentUICulture
.
请注意,CultureInfo.CurrentCulture
影响参数格式,而不是资源选择;资源选择由 控制CultureInfo.CurrentUICulture
。