ICustomFormatter Interface

[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]

Defines a method that supports custom formatting of the value of an object.

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

public interface ICustomFormatter

The ICustomFormatter type exposes the following members.

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

The ICustomFormatter interface includes a single method, ICustomFormatter..::..Format. When this interface is implemented by a reference or value type, the Format method returns a custom-formatted string representation of an object's value.

Typically, the ICustomFormatter interface is implemented with the IFormatProvider interface to customize the behavior of .NET Framework string formatting methods that include an IFormatProvider parameter. For example, the ICustomFormatter interface can provide custom formatting of the value of an object passed to the String.Format(IFormatProvider, String, array<Object>[]()[][]) method.

Providing a custom representation of an object's value requires that you do the following:

  1. Define a class that implements the ICustomFormatter interface and its single member, the Format method.

  2. Define a class that implements the IFormatProvider interface and its single member, the GetFormat method. The GetFormat method returns an instance of your ICustomFormatter implementation. Often, a single class implements both ICustomFormatter and IFormatProvider. In that case, the class's GetFormat implementation just returns an instance of itself.

  3. Pass the IFormatProvider implementation as the provider argument of the String.Format(IFormatProvider, String, array<Object>[]()[][]) method or a comparable method.

Show: