Export (0) Print
Expand All

IFormattable.ToString Method

Formats the value of the current instance using the specified format.

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

string ToString (
	string format,
	IFormatProvider formatProvider
)
String ToString (
	String format, 
	IFormatProvider formatProvider
)
function ToString (
	format : String, 
	formatProvider : IFormatProvider
) : String

Parameters

format

The String specifying the format to use.

-or-

a null reference (Nothing in Visual Basic) to use the default format defined for the type of the IFormattable implementation.

formatProvider

The IFormatProvider to use to format the value.

-or-

a null reference (Nothing in Visual Basic) to obtain the numeric format information from the current locale setting of the operating system.

Return Value

A String containing the value of the current instance in the specified format.

NumberFormatInfo, DateTimeFormatInfo and CultureInfo implement the IFormatProvider interface.

NumberFormatInfo supplies numeric formatting information, such as the characters to use for decimal and thousand separators and the spelling and placement of currency symbols in monetary values.

DateTimeFormatInfo supplies date- and time-related formatting information, such as the position of the month, the day and the year in a date pattern.

CultureInfo contains the default formatting information in a specific culture, including the numeric format information and date- and time-related formatting information.

The following code example demonstrates implementing the ToString method. This code example is part of a larger example provided for the IFormattable class.

public override String ToString() { return ToString(null, null); }

public String ToString(String format, IFormatProvider fp)
{
    // If no format is passed, display like this: (x, y).
    if (format == null) return String.Format("({0}, {1})", x, y);

    // For "x" formatting, return just the x value as a string
    if (format == "x") return x.ToString();

    // For "y" formatting, return just the y value as a string
    if (format == "y") return y.ToString();

    // For any unrecognized format, throw an exception.
    throw new FormatException(String.Format("Invalid format string: '{0}'.", format));
}

Windows 98, Windows 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 .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft