Export (0) Print
Expand All

IFormattable Interface

Updated: April 2009

Provides functionality to format the value of an object into a string representation.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

public interface IFormattable

The IFormattable interface converts an object to its string representation based on a format string and a format provider.

A format string typically defines the general appearance of an object. For example, the .NET Framework supports the following:

A format provider returns a formatting object that typically defines the symbols used in converting an object to its string representation. For example, when you convert a number to a currency value, a format provider defines the currency symbol that appears in the result string. The .NET Framework defines three format providers:

In addition, you can define your own custom format providers to supply culture-specific, profession-specific, or industry-specific information used in formatting. For more information about implementing custom formatting by using a custom format provider, see ICustomFormatter.

The IFormattable interface defines a single method, ToString, that supplies formatting services for the implementing type. The ToString method can be called directly. In addition, it is called automatically by the Convert.ToString(Object) and Convert.ToString(Object, IFormatProvider) methods, and by methods that use the composite formatting feature in the .NET Framework. Such methods include Console.WriteLine(String, Object), String.Format, and StringBuilder.AppendFormat(String, Object), among others. The ToString method is called for each format item in the method's format string.

The IFormattable interface is implemented by the base data types.

Notes to Implementers:

Classes that require more control over the formatting of strings than Object.ToString provides should implement IFormattable.

A class that implements IFormattable must support the "G" (general) format specifier. Besides the "G" specifier, the class can define the list of format specifiers that it supports. In addition, the class must be prepared to handle a format specifier that is a null reference (Nothing in Visual Basic). For more information about formatting and formatting codes, see Formatting Overview

The following example defines a Temperature class that implements the IFormattable interface. The class supports four format specifiers: "G" and "C", which indicate that the temperature is to be displayed in Celsius; "F", which indicates that the temperature is to be displayed in Fahrenheit; and "K", which indicates that the temperature is to be displayed in Kelvin. In addition, the IFormattable.ToString implementation also can handle a format string that is a null reference (Nothing in Visual Basic) or empty. The other two ToString methods defined by the Temperature class simply wrap a call to the IFormattable.ToString implementation.

No code example is currently available or this language may not be supported.

The following example then calls the IFormattable.ToString implementation either directly or by using a composite format string.

No code example is currently available or this language may not be supported.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0

Date

History

Reason

April 2009

Revised and expanded the Remarks section.

Information enhancement.

Community Additions

ADD
Show:
© 2014 Microsoft