Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

IFormatProvider.GetFormat Method

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

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

Object GetFormat (
	Type formatType
)
Object GetFormat (
	Type formatType
)
function GetFormat (
	formatType : Type
) : Object
Not applicable.

Parameters

formatType

An object that specifies the type of format object to return.

Return Value

An instance of the object specified by formatType, if the IFormatProvider implementation can supply that type of object; otherwise, a null reference (Nothing in Visual Basic).

GetFormat is a callback method that formatting and parsing methods invoke to retrieve information about the format of the input string in parsing operations or the format of the output string in formatting operations. In the formatType parameter, the formatting or parsing method passes the type of object it requires to perform its operation. If the IFormatProvider implementation can supply this formatting or parsing object, it returns that object. If not, it returns a null reference (Nothing in Visual Basic).

For example, in the call to the Int32.ToString(IFormatProvider) method, the method argument is an IFormatProvider object that provides information about how the string representation of the current integer instance might be formatted. When the runtime executes the method, it calls the IFormatProvider object's GetFormat method and passes it a Type object that represents the NumberFormatInfo type. If the IFormatProvider object can supply a NumberFormatInfo object, it returns that object. If it cannot supply an object of that type, it returns a null reference (Nothing in Visual Basic).

You can implement the IFormatProvider interface and the GetFormat method in classes that provide custom formatting or parsing services. The IFormatProvider implementation is then passed as an argument to any overload of a parsing or formatting method that has a parameter of type IFormatProvider, such as String.Format(IFormatProvider,String,Object[]), Int32.ToString(String,IFormatProvider), or DateTime.Parse(String,IFormatProvider).

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 itself if the formatType parameter refers to a class that implements ICustomFormatter; otherwise, GetFormat returns a null reference (Nothing in Visual Basic).

An instance of the AcctNumberFormat class can then be passed as an argument to a method that provides formatting or parsing services. For example, the following code passes an AcctNumberFormat class to the String.Format(IFormatProvider,String,Object[]) method to generate 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

Community Additions

Show:
© 2014 Microsoft