System.ICustomFormatter.Format Method

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

Syntax

public string Format (string format, object arg, IFormatProvider formatProvider)

Parameters

format
A format string containing formatting specifications.
arg
An object to format.
formatProvider
An object that supplies format information about the current instance.

Returns

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

Remarks

ICustomFormatter.Format(string, object, IFormatProvider) is a callback method. It is called by a method that supports custom formatting, such as string.Format(IFormatProvider, string, Object[]) or System.Text.StringBuilder.AppendFormat(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(string, object, IFormatProvider) method is called three times.

code reference: System.ICustomFormatter.Class#4

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 null. If format is null, 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 null, ignore that parameter.

Your implementation of the ICustomFormatter.Format(string, object, IFormatProvider) 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(string, IFormatProvider) method of that interface. Otherwise, invoke the default object.ToString method of the underlying object. The following code illustrates this pattern.

code reference: System.ICustomFormatter.Class#3

Requirements

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0