Provides a mechanism for retrieving an object to control formatting.
Assembly: mscorlib (in mscorlib.dll)
Thetype exposes the following members.
The 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 Format. Parsing operations convert the string representation of a value to a type with that value. Typical parsing methods are Parse and TryParse.
The interface consists of a single method, IFormatProvider.GetFormat. GetFormat 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 GetFormat method is responsible for returning an object of that type.
implementations are often used implicitly by formatting and parsing methods. For example, the DateTime.ToString(String) method implicitly uses an implementation that represents the system's current culture. implementations can also be specified explicitly by methods that have a parameter of type , such as Int32.Parse(String, IFormatProvider) and String.Format(IFormatProvider, String, Object).
The .NET Framework includes the following three predefined implementations to provide culture-specific information that is used in formatting or parsing numeric and date and time values:
The 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 NumberFormatInfo object and used in numeric formatting operations, see Standard Numeric Format Strings and Custom Numeric Format Strings.
The 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 DateTimeFormatInfo object and used in numeric formatting operations, see Standard Date and Time Format Strings and Custom Date and Time Format Strings.
The CultureInfo class, which represents a particular culture. Its GetFormat method returns a culture-specific NumberFormatInfo or DateTimeFormatInfo object, depending on whether the 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 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.
The following example illustrates how an implementation can change the representation of a date and time value. In this case, a single date is displayed by using CultureInfo objects that represent four different cultures.
The following example illustrates the use of a class that implements the interface and the GetFormat method. The AcctNumberFormat class converts an Int64 value that represents an account number to a formatted 12-digit account number. Its GetFormat method returns a reference to the current AcctNumberFormat instance if the formatType parameter refers to a class that implements ICustomFormatter; otherwise, GetFormat returns a null reference (Nothing in Visual Basic).
The class that implements can then be used in a call to a formatting and parsing operation. For example, the following code calls the String.Format(IFormatProvider, String, Object) method to generate a string that contains a formatted 12-digit account number.
.NET FrameworkSupported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0
.NET Framework Client ProfileSupported in: 4, 3.5 SP1
Portable Class LibrarySupported in: Portable Class Library
.NET for Windows Store appsSupported in: Windows 8
Supported in: Windows Phone 8.1
Supported in: Windows Phone Silverlight 8.1
Supported in: Windows Phone Silverlight 8
Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)