IFormattable Interface
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Provides functionality to format the value of an object into a string representation.
Assembly: mscorlib (in mscorlib.dll)
The IFormattable type exposes the following members.
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:
Standard format strings for formatting enumeration values (see Enumeration Format Strings).
Standard and custom format strings for formatting numeric values (see Standard Numeric Format Strings and Custom Numeric Format Strings).
Standard and custom format strings for formatting date and time values (see Standard Date and Time Format Strings and Custom Date and Time Format Strings).
You can also define your own format strings to support formatting of your application-defined types.
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:
The System.Globalization::CultureInfo class, which returns either a NumberFormatInfo object for formatting numeric values, or a DateTimeFormatInfo object for formatting date and time values.
The System.Globalization::NumberFormatInfo class, which returns an instance of itself for formatting numeric values.
The System.Globalization::DateTimeFormatInfo class, which returns an instance of itself for formatting date and time values.
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 in the .NET Framework. Such methods include String::Format and StringBuilder::AppendFormat, among others. The ToString method is called for each format item in the method's format string.
IFormattable is implemented by the base data types.
Notes to ImplementersClasses 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) formatting code. Besides the "G" code, the class can define the list of formatting codes that it supports. In addition, the class must be prepared to handle a format specifier that is nullptr. For more information on formatting and formatting codes, see Formatting Types.
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 nullptr or empty. The other two ToString methods defined by the Temperature class simply wrap a call to the IFormattable::ToString implementation.
The following example then calls the IFormattable::ToString implementation either directly or by using a composite format string.
