Export (0) Print
Expand All
4 out of 25 rated this helpful - Rate this topic

IFormatProvider Interface

Provides a mechanism for retrieving an object to control formatting.

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

[ComVisibleAttribute(true)] 
public interface IFormatProvider
/** @attribute ComVisibleAttribute(true) */ 
public interface IFormatProvider
ComVisibleAttribute(true) 
public interface IFormatProvider
Not applicable.

Formatting methods convert the value of a type to the string representation of that value. Parsing methods convert the string representation of a value to a type with that value. These methods use the GetFormat method of types that implement the IFormatProvider interface to obtain an object that supplies information used in the formatting or parsing operation. The IFormatProvider interface imposes no requirements or restrictions on the information that is supplied.

A type that implements the IFormatProvider interface typically supplies culture-specific information. For example, the IFormatProvider interface is implemented by the NumberFormatInfo, DateTimeFormatInfo, and CultureInfo classes. The NumberFormatInfo class provides information that is used to format numbers, such as the currency, thousands separator, and decimal separator symbols for a particular culture. The DateTimeFormatInfo class provides information that is used to format dates and times, such as the date and time separator symbols for a particular culture. The CultureInfo class provides both a NumberFormatInfo object and a DateTimeFormatInfo object for a particular culture.

IFormatProvider implementations are typically passed as parameters to methods that perform custom formatting or parsing operations. Methods that perform custom formatting operations include String.Format(IFormatProvider,String,Object[]) and various ToString instance methods. Methods that perform custom parsing operations include the Parse and TryParse methods of date, time, and numeric types.

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 a null reference (Nothing in Visual Basic).

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,Object[]) method to generate a string that contains a formatted 12-digit account number.

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.