See Also: IFormatProvider Members
The IFormatProvider interface supplies an object that provides formatting information for formatting and parsing operations. Formatting operations convert the value of a type to the string representation of that value. Typical formatting methods are the ToString methods of a type, as well as string.Format(string, object). Parsing operations convert the string representation of a value to a type with that value. Typical parsing methods are Parse and TryParse.
The IFormatProvider interface consists of a single method, IFormatProvider.GetFormat(Type). IFormatProvider.GetFormat(Type) is a callback method: The parsing or formatting method calls it and passes it a Type object that represents the type of object that the formatting or parsing method expects will provide formatting information. The IFormatProvider.GetFormat(Type) method is responsible for returning an object of that type.
IFormatProvider implementations are often used implicitly by formatting and parsing methods. For example, the DateTime.ToString(string) method implicitly uses an IFormatProvider implementation that represents the system's current culture. IFormatProvider implementations can also be specified explicitly by methods that have a parameter of type IFormatProvider, such as int.Parse(string, IFormatProvider) and string.Format(IFormatProvider, string, Object).
The .NET Framework includes the following three predefined IFormatProvider implementations to provide culture-specific information that is used in formatting or parsing numeric and date and time values:
The System.Globalization.NumberFormatInfo class, which provides information that is used to format numbers, such as the currency, thousands separator, and decimal separator symbols for a particular culture. For information about the predefined format strings recognized by a System.Globalization.NumberFormatInfo object and used in numeric formatting operations, see Standard Numeric Format Strings and Custom Numeric Format Strings.
The System.Globalization.DateTimeFormatInfo class, which provides information that is used to format dates and times, such as the date and time separator symbols for a particular culture or the order and format of a date's year, month, and day components. For information about the predefined format strings recognized by a System.Globalization.DateTimeFormatInfo object and used in numeric formatting operations, see Standard Date and Time Format Strings and Custom Date and Time Format Strings.
The System.Globalization.CultureInfo class, which represents a particular culture. Its IFormatProvider.GetFormat(Type) method returns a culture-specific System.Globalization.NumberFormatInfo or System.Globalization.DateTimeFormatInfo object, depending on whether the System.Globalization.CultureInfo object is used in a formatting or parsing operation that involves numbers or dates and times.
The .NET Framework also supports custom formatting. This typically involves the creation of a formatting class that implements both IFormatProvider and ICustomFormatter. An instance of this class is then passed as a parameter to a method that performs a custom formatting operation, such as string.Format(IFormatProvider, string, Object) The example provides an illustration of such a custom implementation that formats a number as a 12-digit account number.