Duration (Pacific Standard Time):
To (Pacific Standard Time):
Impact:
  • None
User Action:
  • None
.NET Framework Class Library

IFormattable.ToString Method

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

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
Function ToString ( _
	format As String, _
	formatProvider As IFormatProvider _
) As String

Parameters

format
Type: System.String

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
Type: System.IFormatProvider

The provider 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

Type: System.String
The value of the current instance in the specified format.

The ToString method converts a value to a string representation that can be expressed in multiple ways. Its precise format depends on specific symbols or a specified order defined by specific cultures, professions, or industries. You can call the method directly. It is also called automatically by the Convert.ToString(Object) and Convert.ToString(Object, IFormatProvider) methods, and by methods that use the composite formatting feature in the .NET Framework, such as String.Format(String, Object[]), Console.WriteLine(String, Object[]), and StringBuilder.AppendFormat(String, Object[]). (For more information, see Composite Formatting.)

Composite formatting methods call the ToString method once for each format item in a format string. The parameters passed to the method depend on the specific formatting method that is called and on the content of the format item, as follows:

Note Note

An object's ToString implementation is called by composite formatting methods only if they are not passed an ICustomFormatter format provider, or if the Format method of the custom format provider returns null.

The .NET Framework includes three format providers, all of which implement the IFormatProvider interface:

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

  • DateTimeFormatInfo supplies date-related 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-related and time-related formatting information.

In addition, you can define your own custom format provider.

Notes to Implementers

The ToString method must support the "G" (general) format specifier. Besides the "G" specifier, the class can define the list of format specifiers that it supports. In addition, the class must be prepared to handle a format specifier that is null. For more information about formatting and formatting codes, see Formatting Types in the .NET Framework.

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

Imports System.Globalization

Public Class Temperature : Implements IFormattable
   Private temp As Decimal 

   Public Sub New(temperature As Decimal)
      If temperature < -273.15 Then _ 
        Throw New ArgumentOutOfRangeException(String.Format("{0} is less than absolute zero.", _
                                              temperature))
      Me.temp = temperature
   End Sub 

   Public ReadOnly Property Celsius As Decimal 
      Get 
         Return temp
      End Get 
   End Property 

   Public ReadOnly Property Fahrenheit As Decimal 
      Get 
         Return temp * 9 / 5 + 32
      End Get 
   End Property 

   Public ReadOnly Property Kelvin As Decimal 
      Get 
         Return temp + 273.15d
      End Get 
   End Property 

   Public Overrides Function ToString() As String 
      Return Me.ToString("G", CultureInfo.CurrentCulture)
   End Function 

   Public Overloads Function ToString(fmt As String) As String 
      Return Me.ToString(fmt, CultureInfo.CurrentCulture)
   End Function 

   Public Overloads Function ToString(fmt As String, provider As IFormatProvider) _
                   As String _
                   Implements IFormattable.ToString
      If String.IsNullOrEmpty(fmt) Then fmt = "G" 
      If provider Is Nothing Then provider = CultureInfo.CurrentCulture

      Select Case fmt.ToUpperInvariant()
         Case "G", "C" 
            Return temp.ToString("F2", provider) + " °C"  
         Case "F" 
            Return Fahrenheit.ToString("F2", provider) + " °F" 
         Case "K" 
            Return Kelvin.ToString("F2", provider) + " K" 
         Case Else 
            Throw New FormatException(String.Format("The {0} format string is not supported.", fmt))
      End Select 
   End Function 
End Class

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.