文化不变对象 ToString()
如何在对象上调用 ToString()
并使其使用不变区域性? 实现 IConvertible
的对象(如 bool、int、float..)上有 ToString()
重载,但如果相关对象不是 IConvertible,该怎么办?
How can I call ToString()
on an object and make it use the invariant culture?
There are overloads for ToString()
on objects that implement IConvertible
(like bool, int, float..), but what if the object in question is not IConvertible
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
System.Convert
类有一个静态ToString
重载,它接受一个object
。根据我的基准,这大约是
string.Format(CultureInfo. InvariantCulture,“{0}”,值)
,更重要的是,它看起来更干净。但是,如果您已经在构建字符串,我建议您使用 FormattableString.Invariant。The
System.Convert
class has a staticToString
overload that takes anobject
.Based on my benchmarks, this is roughly twice as fast as
string.Format(CultureInfo.InvariantCulture, "{0}", value)
and, more importantly, it looks cleaner. However, if you are building a string already, I'd recommend FormattableString.Invariant.我认为
IFormattable
是相关接口。它有一个ToString
方法,可让您指定格式提供程序,该提供程序可以是一种区域性。I think
IFormattable
is the relevant interface. It has aToString
method that lets you specify the format provider, which can be a culture.