IFormattable.ToString Method
Assembly: mscorlib (in mscorlib.dll)
String ToString ( String format, IFormatProvider formatProvider )
function ToString (
format : String,
formatProvider : IFormatProvider
) : String
Not applicable.
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 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.