在 C# 中,在对字符串或字符使用 ToString() 时,在什么情况下我需要提供 CultureInfo IFormatProvider?

With C# under what circumstances do I need to supply a CultureInfo IFormatProvider when using ToString() on a String or Char?

使用 C# 在 StringChar 上使用 ToString() 时,在什么情况下我需要提供 CultureInfo IFormatProvider

我通过数字或日期转换得到它,例如

(-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"));

给予:

-10000,4
-10000.4
01/05/2018 22:49:09
5/1/2018 10:49:09 PM

但是以下情况呢:

"Some string".ToString(provider: new System.Globalization.CultureInfo("es-ES"));
"Some string".ToString(provider: new System.Globalization.CultureInfo("en-US"));

我可以使用哪些会受文化影响的字符串?

char 相同。

在任何情况下,因为它什么都不做。作为 String.ToString(IFormatProvider provider)MSDN documentation 状态:

provider is reserved, and does not currently participate in this operation.

Because this method simply returns the current string unchanged, there is no need to call it directly.

同样适用于Char.ToString(IFormatProvider provider)


StringChar之所以有这种看似无用的方法是因为 需要实现 IConvertible 接口,例如允许从 stringintintstring 等的转换