简体   繁体   English

是否可以对内置类型的object.ToString()和IFormattable.ToString(string,IFormatProvider)之间的关系做出任何假设?

[英]Can any assumptions be made about the relationship between object.ToString() and IFormattable.ToString(string, IFormatProvider) for built-in types?

I'm writing some string output formatting code for aggregated records (think CSV format output). 我正在为汇总记录编写一些字符串输出格式代码(请考虑CSV格式输出)。 I'm trying to write it so that it leverages the built-in string formatting capabilities of many types via the IFormattable.ToString(string, IFormatProvider) interface, in addition to the simple object.ToString() . 我想,这样它利用内置的字符串通过格式化多种类型的功能,把它写IFormattable.ToString(string, IFormatProvider)接口,除了简单的object.ToString()

To reduce code duplication, it would be nice to be able to make some assumptions about the relationship between object.ToString() and IFormattable.ToString(string, IFormatProvider) . 为了减少代码重复,最好能够对IFormattable.ToString(string, IFormatProvider) object.ToString()IFormattable.ToString(string, IFormatProvider)之间的关系进行一些假设。

For example, could it be relied upon to assume that ToString() == ToString(null, null) ? 例如,是否可以假设ToString() == ToString(null, null) Is there a default culture or format provider that maintains that mapping? 是否有默认的区域性或格式提供程序来维护该映射? Or is there no necessary relationship between the two? 还是两者之间没有必要的关系?

Based on the MSDN documentation and .NET source code you can assume that for built-in types ToString() is equivalent to ToString(null, null) and ToString("G", null) . 根据MSDN文档和.NET源代码,您可以假定内置类型ToString()等效于ToString(null, null)ToString("G", null)

There is some information about it in Formatting Types in the .NET Framework on MSDN. 在MSDN上的.NET Framework中的“ 格式化类型”中有关于此的一些信息。

For example according to that site Int32.ToString() 例如根据该站点Int32.ToString()

Calls Int32.ToString("G", NumberFormatInfo.CurrentInfo) to format the Int32 value for the current culture. 调用Int32.ToString("G", NumberFormatInfo.CurrentInfo)格式化当前区域性的Int32值。

If you check the source code , you will notice that ToString() calls 如果检查源代码 ,您会注意到ToString()调用

Number.FormatInt32(m_value, null, NumberFormatInfo.CurrentInfo);

while String ToString(String format, IFormatProvider provider) calls String ToString(String format, IFormatProvider provider)调用

Number.FormatInt32(m_value, format, NumberFormatInfo.GetInstance(provider));

So the format is actually null , not "G" . 因此, format实际上是null ,而不是"G" It doesn't make a difference though, as "G" and null should be the same. 不过,这没有什么区别,因为"G"null应该相同。 NumberFormatInfo.GetInstance(null) returns NumberFormatInfo.CurrentInfo , so Int32.ToString() is equivalent to Int32.ToString("G", null) or Int32.ToString(null, null) . NumberFormatInfo.GetInstance(null)返回NumberFormatInfo.CurrentInfo ,因此Int32.ToString()等效于Int32.ToString("G", null)Int32.ToString(null, null)

You can double check it with IFormattable.ToString documentation to see that null s indeed indicate default values for both parameters. 您可以使用IFormattable.ToString文档进行仔细检查,以确保null确实表示两个参数的默认值。

Parameters 参数

format 格式

The format to use. 使用的格式。

-or- -要么-

A null reference (Nothing in Visual Basic) to use the default format defined for the type of the IFormattable implementation. 一个空引用(在Visual Basic中为Nothing),以使用为IFormattable实现的类型定义的默认格式。

formatProvider formatProvider

The provider to use to format the value. 用于格式化值的提供程序。

-or- -要么-

A null reference (Nothing in Visual Basic) to obtain the numeric format information from the current locale setting of the operating system. 一个空引用(在Visual Basic中为Nothing),用于从操作系统的当前区域设置中获取数字格式信息。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM