System.IFormattable 接口

方法描述

提供将对象的值格式化为字符串表示形式的功能。

语法定义(C# System.IFormattable 接口 的用法)

[ComVisibleAttribute(true)]
public interface IFormattable

构造函数

构造函数名称 构造函数描述

成员/方法

方法名称 方法描述
ToString 使用指定的格式格式化当前实例的值。

提示和注释

IFormattable 接口基于格式字符串和格式提供程序,将对象转换为其字符串表现形式。

格式字符串通常定义对象的常规外观。 例如,.NET Framework 支持下列:

用于枚举值格式设置的标准格式字符串(请参见 枚举格式字符串)。

用于数字值格式设置的标准和自定义格式字符串(请参见 标准数字格式字符串 和 自定义数字格式字符串)。

用于日期和时间值格式设置的标准和自定义格式字符串(请参见 标准日期和时间格式字符串 和 自定义日期和时间格式字符串)。

用于时间间隔格式设置的标准和自定义格式字符串(请参见 标准 TimeSpan 格式字符串 和 自定义的 TimeSpan 格式字符串)。

还可以定义自己的格式字符串,以便支持应用程序所定义的类型的格式设置。

格式提供程序返回一个格式设置对象,该对象通常定义用于将对象转换为其字符串表示形式的符号。 例如,将数字转换为货币值时,定义货币符号的格式提供程序出现在结果字符串中。 .NET Framework 定义了三种格式提供程序:

System.Globalization.CultureInfo 类,返回 NumberFormatInfo 对象用于设置数字值格式,或者返回 DateTimeFormatInfo 对象用于设置日期和时间值的格式。

System.Globalization.NumberFormatInfo 类,返回自身的实例用于设置数字值的格式。

System.Globalization.DateTimeFormatInfo 类,返回自身的实例用于设置日期和时间值的格式。

此外,您可以定义您自己的自定义格式提供程序,以提供在格式中使用的区域性特定、职业特定或行业特定的信息。 有关通过使用自定义格式提供程序实现自定义格式的更多信息,请参见 ICustomFormatter。

IFormattable 接口定义一个方法 ToString,为实现类型提供格式设置服务。 ToString 方法可直接调用。 此外,它通过 Convert.ToString(Object) 和 Convert.ToString(Object, IFormatProvider) 方法以及使用 .NET Framework 中 复合格式设置功能的方法自动调用。 其中,这些方法包括 Console.WriteLine(String, Object)、String.Format 和 StringBuilder.AppendFormat(String, Object)。 ToString 方法为方法的格式字符串中每一个格式项进行调用。

IFormattable 接口由基础数据类型实现。

对实现者的说明

需要的字符串格式化控件比 Object.ToString 提供的多的类应实现 IFormattable。

实现 IFormattable 的类必须支持“G”(常规)格式说明符。 除“G”说明符外,该类还可以定义它支持的格式说明符的列表。 此外,类必须准备处理的是 null 格式说明符。 有关格式化和格式化代码的更多信息,请参见 格式化类型。

System.IFormattable 接口例子

下面的示例则直接或使用复合格式字符串调用 IFormattable.ToString 实现。

public class Example
{
   public static void Main()
   {
      // Use composite formatting with format string in the format item.
      Temperature temp1 = new Temperature(0);
      Console.WriteLine("{0:C} (Celsius) = {0:K} (Kelvin) = {0:F} (Fahrenheit)\n", temp1);

      // Use composite formatting with a format provider.
      temp1 = new Temperature(-40);
      Console.WriteLine(String.Format(CultureInfo.CurrentCulture, "{0:C} (Celsius) = {0:K} (Kelvin) = {0:F} (Fahrenheit)", temp1));
      Console.WriteLine(String.Format(new CultureInfo("fr-FR"), "{0:C} (Celsius) = {0:K} (Kelvin) = {0:F} (Fahrenheit)\n", temp1));

      // Call ToString method with format string.
      temp1 = new Temperature(32);
      Console.WriteLine("{0} (Celsius) = {1} (Kelvin) = {2} (Fahrenheit)\n", 
                        temp1.ToString("C"), temp1.ToString("K"), temp1.ToString("F"));

      // Call ToString with format string and format provider
      temp1 = new Temperature(100)      ;
      NumberFormatInfo current = NumberFormatInfo.CurrentInfo;
      CultureInfo nl = new CultureInfo("nl-NL"); 
      Console.WriteLine("{0} (Celsius) = {1} (Kelvin) = {2} (Fahrenheit)", 
                        temp1.ToString("C", current), temp1.ToString("K", current), temp1.ToString("F", current));
      Console.WriteLine("{0} (Celsius) = {1} (Kelvin) = {2} (Fahrenheit)", 
                        temp1.ToString("C", nl), temp1.ToString("K", nl), temp1.ToString("F", nl));
   }
}
// The example displays the following output:
//    0.00 °C (Celsius) = 273.15 K (Kelvin) = 32.00 °F (Fahrenheit)
//    
//    -40.00 °C (Celsius) = 233.15 K (Kelvin) = -40.00 °F (Fahrenheit)
//    -40,00 °C (Celsius) = 233,15 K (Kelvin) = -40,00 °F (Fahrenheit)
//    
//    32.00 °C (Celsius) = 305.15 K (Kelvin) = 89.60 °F (Fahrenheit)
//    
//    100.00 °C (Celsius) = 373.15 K (Kelvin) = 212.00 °F (Fahrenheit)
//    100,00 °C (Celsius) = 373,15 K (Kelvin) = 212,00 °F (Fahrenheit)

继承层次结构

命名空间

namespace: System

程序集: mscorlib(在 mscorlib.dll 中)

线程安全

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0、1.1、1.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1 受以下版本支持:

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。