IFormatProvider Interface


Provides a mechanism for retrieving an object to control formatting.

Namespace:   System
Assembly:  mscorlib (in mscorlib.dll)

public interface class IFormatProvider


Returns an object that provides formatting services for the specified type.

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 Format. 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. 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.

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 Int32::Parse(String^, IFormatProvider^) and String::Format(IFormatProvider^, String^, array<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 .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^, array<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 IFormatProvider 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.

No code example is currently available or this language may not be supported.

The following example illustrates the use of a class that implements the IFormatProvider 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 null.

No code example is currently available or this language may not be supported.

The class that implements IFormatProvider can then be used in a call to a formatting and parsing operation. For example, the following code calls the String::Format(IFormatProvider^, String^, array<Object^>^) method to generate a string that contains a formatted 12-digit account number.

No code example is currently available or this language may not be supported.

Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top