[英]With C# under what circumstances do I need to supply a CultureInfo IFormatProvider when using ToString() on a String or Char?
With C# under what circumstances do I need to supply a CultureInfo
IFormatProvider
when using ToString()
on a String
or Char
? 使用C#在String
或Char
上使用ToString()
时,在什么情况下需要提供CultureInfo
IFormatProvider
?
I get it with number or date conversion, eg 我通过数字或日期转换得到它,例如
(-10000.4).ToString(provider: new System.Globalization.CultureInfo("es-ES"));
(-10000.4).ToString(provider: new System.Globalization.CultureInfo("en-US"));
DateTime.Now.ToString(provider: new System.Globalization.CultureInfo("es-ES"));
DateTime.Now.ToString(provider: new System.Globalization.CultureInfo("en-US"));
giving: 赠送:
-10000,4
-10000.4
01/05/2018 22:49:09
5/1/2018 10:49:09 PM
but what about the following: 但是以下内容如何:
"Some string".ToString(provider: new System.Globalization.CultureInfo("es-ES"));
"Some string".ToString(provider: new System.Globalization.CultureInfo("en-US"));
What strings could I possibly use that would be affected by culture? 我可能会使用哪些字符串受文化影响?
Same for char
. 同样的char
。
Under no circumstance, since it does not do anything. 在任何情况下,因为它什么都不做。 As the MSDN documentatio n for String.ToString(IFormatProvider provider)
states: 作为String.ToString(IFormatProvider provider)
的MSDN文档说明:
provider
is reserved, and does not currently participate in this operation.provider
是保留的,目前不参与此操作。Because this method simply returns the current string unchanged, there is no need to call it directly. 因为此方法只是返回当前字符串不变,所以不需要直接调用它。
The same applies to Char.ToString(IFormatProvider provider)
. 这同样适用于Char.ToString(IFormatProvider provider)
。
The reason why String
and Char
have this apparently useless method is that it is needed to implement the IConvertible
interface, that eg allows conversion from string
to int
, int
to string
etc. String
和Char
有这个看似无用的方法的原因是它需要实现IConvertible
接口,例如允许从string
转换为int
, int
转换为string
等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.