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:
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:
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 null. For more information about formatting and formatting codes, see Formatting Types