.ToString()和.ToString(CultureInfo.CurrentCulture) [英] .ToString() and .ToString(CultureInfo.CurrentCulture)

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

问题描述

例如,我必须在每个数字中使用 .ToString(CultureInfo.CurrentCulture)进行字符串转换.我是否可以以某种方式覆盖 .ToString(),以便在字符串转换中不再收到像区域性这样的消息?

I must use .ToString(CultureInfo.CurrentCulture) in every number to string conversion for example. Can I somehow override .ToString() so that I will get no more messages like culture in string conversion explicitly?

例如,现在我必须更改每一个

For example, now I have to change every

myNumValue.Count.ToString();

myNumValue.Count.ToString(CultureInfo.CurrentCulture);

推荐答案

此规则的重点是为每种格式设置和解析操作做出显式区域性选择.如果您试图系统地交换实现以欺骗"规则,那么您将最终无所适从.正如Lonli-Lokli所建议的那样,如果您不喜欢阅读带有区域性参数的 ToString 调用,则可能要考虑使用扩展方法,但是对于所支持的每种区域性,都应该有一个独特的扩展方法.例如: ToUIString()-> ToString(CultureInfo.CurrentCulture) ToInvariantString()-> ToString(CultureInfo.InvariantCulture)

The point of this rule is to make an explicit choice of culture for every formatting and parsing operation. If you try to systematically swap implementations to "trick" the rule, you'll simply end up sweeping potential problems under the rug. As suggested by Lonli-Lokli, you may want to consider using extension methods if you don't like reading ToString calls with a culture arguments, but you should have a distinct extension method for each culture you support. e.g.: ToUIString() -> ToString(CultureInfo.CurrentCulture) and ToInvariantString() -> ToString(CultureInfo.InvariantCulture)

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

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