是CultureInfo.CurrentCulture中的String.Format真的有必要()? [英] Is CultureInfo.CurrentCulture really necessary in String.Format()?

查看:159
本文介绍了是CultureInfo.CurrentCulture中的String.Format真的有必要()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你怎么认为确有必要提供的IFormatProvider 的方法的String.Format(字符串对象)

How do you think is really necessary to provide IFormatProvider in method String.Format(string, object) ?

是更好地写满变种

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

或只是

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

推荐答案

在一般情况下,你将要使用InvariantCulture的,如果你正在生成的字符串的方式,独立于当前用户的文化被持久化(例如,在注册表,或者在一个文件中)。

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

您将要使用的CurrentCulture对于要成为psented在UI到当前用户$ P $(窗体,报表)的字符串。

You will want to use CurrentCulture for strings that are to be presented in the UI to the current user (forms, reports).

可能会出现微妙的错误,如果你使用的CurrentCulture,你应该使用InvariantCulture的:虫子,只有大白于天下,当你有多个用户与不同文化背景的访问相同的注册表项或文件,或者如果用户改变了默认的文化

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.

显式指定的CurrentCulture(默认如果省略的IFormatProvider参数),本质上是证明你已经考虑了以上和正在生成的字符串应该使用当前用户的区域性文件。这就是为什么的FxCop建议您应指定IFormatProvider的参数。

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.

这篇关于是CultureInfo.CurrentCulture中的String.Format真的有必要()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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