简体   繁体   English

CultureInfo.CurrentCulture在String.Format()中是否真的必要?

[英]Is CultureInfo.CurrentCulture really necessary in String.Format()?

How do you think is really necessary to provide IFormatProvider in method String.Format(string, object) ? 你认为在方法String.Format(string, object)提供IFormatProvider真的有什么必要吗?

Is it better to write full variant 编写完整变体是否更好

String.Format(CultureInfo.CurrentCulture, "String is {0}", str);

or just 要不就

String.Format("String is {0}", str);

?

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 (eg 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. 显式指定CurrentCulture(如果省略IFormatProvider参数,则为默认值),本质上是文档,表明您已经考虑了上述内容,并且生成的字符串应该使用当前用户的文化。 That's why FxCop recommends that you should specify the IFormatProvider argument. 这就是为什么FxCop建议你应该指定IFormatProvider参数。

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 . 如果未指定IFormatProvider (或等效地传递null ),则大多数参数类型最终将根据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 ,它可以在格式化参数时获得第一次破解,或者根据其他上下文覆盖格式化文化。

Note that CultureInfo.CurrentCulture affects argument formatting, not resource selection; 请注意, CultureInfo.CurrentCulture会影响参数格式,而不会影响资源选择; resource selection is controlled by CultureInfo.CurrentUICulture . 资源选择由CultureInfo.CurrentUICulture控制。

不,您不需要指定文化,除非您的字符串包含文化特定元素,如小数分隔符,货币等,必须根据文化呈现。

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. 也就是说,如果您希望您的应用支持多种语言和特定文化格式,那么您应该使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在CultureInfo.CurrentCulture中使用格式说明符 - Use format specifier with CultureInfo.CurrentCulture CultureInfo.CurrentCulture 错误 - CultureInfo.CurrentCulture wrong TimeSpan.ParseExact(“ 000000.000”,“ hhmmss.fff”,CultureInfo.CurrentCulture); 输入的字符串格式不正确C#? - TimeSpan.ParseExact(“000000.000”, “hhmmss.fff”, CultureInfo.CurrentCulture); Input string was not in a correct format C#? 如何设置 CultureInfo.CurrentCulture? - How to set CultureInfo.CurrentCulture? ToString(CultureInfo.CurrentCulture)这需要吗? - ToString(CultureInfo.CurrentCulture) Is this needed? .ToString() 和 .ToString(CultureInfo.CurrentCulture) - .ToString() and .ToString(CultureInfo.CurrentCulture) JavaScriptSerializer不使用CultureInfo.CurrentCulture - JavaScriptSerializer not using CultureInfo.CurrentCulture CultureInfo.CurrentCulture给了我错误的文化 - CultureInfo.CurrentCulture is giving me the wrong culture 在 WPF 绑定中使用“真正的” CultureInfo.CurrentCulture,而不是来自 IetfLanguageTag 的 CultureInfo - Use “real” CultureInfo.CurrentCulture in WPF Binding, not CultureInfo from IetfLanguageTag CultureInfo.CurrentCulture不包含正确的值->数字格式问题 - CultureInfo.CurrentCulture does not contain the correct value --> numberformat problems
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM