IFormattable.ToString Method
Microsoft Silverlight will reach end of support after October 2021. Learn more.
Formats the value of the current instance using the specified format.
Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Syntax
'Declaration
Function ToString ( _
format As String, _
formatProvider As IFormatProvider _
) As String
string ToString(
string format,
IFormatProvider formatProvider
)
Parameters
- format
Type: System.String
The String specifying the format to use.
-or-
nulla null reference (Nothing in Visual Basic) to use the default format defined for the type of the IFormattable implementation.
- formatProvider
Type: System.IFormatProvider
The IFormatProvider to use to format the value.
-or-
nulla null reference (Nothing in Visual Basic) to obtain the numeric format information from the current locale setting of the operating system.
Return Value
Type: System.String
A String containing the value of the current instance in the specified format.
Remarks
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.
Examples
The following code example demonstrates implementing the ToString method. This code example is part of a larger example provided for the IFormattable class.
Public Overrides Function ToString() As String
Return ToString(Nothing, Nothing)
End Function
Public Overloads Function ToString(ByVal format As String) As String
Return ToString(format, Nothing)
End Function
Public Overloads Function ToString(ByVal format As String, _
ByVal fp As IFormatProvider) As String _
Implements IFormattable.ToString
If String.IsNullOrEmpty(format) Then format = "G"
' If G format specifier is passed, display like this: (x, y).
If format.ToLower() = "g" Then _
Return String.Format("({0}, {1})", x, y)
' For "x" format specifier, return just the x value as a string
If format.ToLower = "x" Then _
Return x.ToString()
' For "y" format specifier, return just the y value as a string
If format.ToLower = "y" Then _
Return y.ToString()
' For any unrecognized format, throw an exception.
Throw New FormatException(String.Format("Invalid format string: '{0}'.", format))
End Function
public override String ToString() { return ToString(null, null); }
public String ToString(String format, IFormatProvider fp)
{
if (String.IsNullOrEmpty(format))
format = "G";
// If G format specifier is passed, display like this: (x, y).
if (format.ToLower() == "g") return String.Format("({0}, {1})", x, y);
// For "x" format specifier, return just the x value as a string
if (format.ToLower() == "x") return x.ToString();
// For "y" format specifier, return just the y value as a string
if (format.ToLower() == "y") return y.ToString();
// For any unrecognized format specifier, throw an exception.
throw new FormatException(String.Format("Invalid format string: '{0}'.", format));
}
Version Information
Silverlight
Supported in: 5, 4, 3
Silverlight for Windows Phone
Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0
XNA Framework
Supported in: Xbox 360, Windows Phone OS 7.0
Platforms
For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.