简体   繁体   English

使用C#在String或Char上使用ToString()时,在什么情况下需要提供CultureInfo IFormatProvider?

[英]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#在StringChar上使用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. StringChar有这个看似无用的方法的原因是它需要实现IConvertible接口,例如允许从string转换为intint转换为string等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 调用ToString(IFormatProvider)时发生C#反射,Reflection.TargetException - C# Reflection, Reflection.TargetException, when Invoke ToString(IFormatProvider) 对ToString(String,IFormatProvider)使用自定义格式 - Using Custom format With ToString(String, IFormatProvider) DateTime.ToString(String,IFormatProvider)不适用于Xamarin.Forms中的Farsi CultureInfo“fa-IR” - DateTime.ToString(String, IFormatProvider) doesn't work for Farsi CultureInfo “fa-IR” in Xamarin.Forms C#:IFormattable、IFormatProvider 和 ICustomFormatter 之间的连接,以及何时使用什么 - C#: Connection between IFormattable, IFormatProvider and ICustomFormatter, and when to use what C#中ToString()的需求是什么? - What is the need of ToString() in C#? 在什么情况下拳击会在现代C#中出现? - Under what circumstances would boxing occur in modern C#? 什么情况下object.ToString()应该返回null? - Under what circumstances should `object.ToString()` return null? C#字符串迄今与CultureInfo - C# String to date with CultureInfo 如何在不使用 ToString() 的情况下在 C# 中将 Int 转换为 String? - How do I convert an Int to a String in C# without using ToString()? 为什么在使用条件运算符时需要显式的ToString()? - Why do I need an explicit ToString() when using a conditional operator?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM