This documentation is archived and is not being maintained.

ICustomFormatter.Format Method

Updated: February 2009

Converts the value of a specified object to an equivalent string representation using specified format and culture-specific formatting information.

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

function Format(
	format : String, 
	arg : Object, 
	formatProvider : IFormatProvider
) : String


Type: System.String

A format string containing formatting specifications.

Type: System.Object

An object to format.

Type: System.IFormatProvider

An object that supplies format information about the current instance.

Return Value

Type: System.String
The string representation of the value of arg, formatted as specified by format and formatProvider.

ICustomFormatter.Format is a callback method. It is called by a method that supports custom formatting, such as String.Format(IFormatProvider, String, Object[]). The implementation is called once for each format item in a composite format string. For example, in the following statement, the ICustomFormatter.Format method is called three times.

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

The arg parameter is the object in the object list whose zero-based position corresponds to the index of a particular format item.

The format parameter contains a format string, which is the formatString component of a format item. If the format item has no formatString component, the value of format is a null reference (Nothing in Visual Basic). If format is a null reference (Nothing in Visual Basic), depending on the type of arg, you may be able to use the default format specification of your choice.

The formatProvider parameter is the IFormatProvider implementation that provides formatting for arg. Typically, it is an instance of your ICustomFormatter implementation. If formatProvider is a null reference (Nothing in Visual Basic), ignore that parameter.

Your implementation of the Format method must include the following functionality so the .NET Framework can provide formatting you do not support. If your format method does not support a format, determine whether the object being formatted implements the IFormattable interface. If it does, invoke the IFormattable.ToString method of that interface. Otherwise, invoke the default Object.ToString method of the underlying object. The following code illustrates this pattern.

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

The following example implements ICustomFormatter to allow binary, octal, and hexadecimal formatting of integral values. Its ICustomFormatter.Format implementation determines whether the format parameter is one of the three supported format strings ("B" for binary, "O" for octal, and "H" for hexadecimal) and formats the arg parameter appropriately. Otherwise, if arg is not a null reference (Nothing in Visual Basic), it calls the arg parameter's IFormattable.ToString implementation, if one exists, or its parameterless ToString method, if one does not. If arg is a null reference (Nothing in Visual Basic), the method returns String.Empty.

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

BinaryFormatter can then be used to provide custom formatting by passing a BinaryFormatter object as the provider parameter of the Format method, as the following example shows.

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

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0




February 2009

Expanded the Remarks section and added an example.

Customer feedback.