See Also: IFormattable 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).
Standard and custom format strings for formatting time intervals (see Standard TimeSpan Format Strings and Custom TimeSpan 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 System.Globalization.NumberFormatInfo object for formatting numeric values, or a System.Globalization.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, IFormattable.ToString(string, IFormatProvider), that supplies formatting services for the implementing type. The IFormattable.ToString(string, IFormatProvider) 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(string, Object[]), and System.Text.StringBuilder.AppendFormat(string, object), among others. The IFormattable.ToString(string, IFormatProvider) method is called for each format item in the method's format string.
The IFormattable interface is implemented by the base data types.