Convert.ToString Method (Object, IFormatProvider)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Converts the value of the specified Object to its equivalent String representation using the specified culture-specific formatting information.

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

Syntax

'Declaration
Public Shared Function ToString ( _
    value As Object, _
    provider As IFormatProvider _
) As String
public static string ToString(
    Object value,
    IFormatProvider provider
)

Parameters

Return Value

Type: System.String
The String representation of the value of value, or String.Empty if value is nulla null reference (Nothing in Visual Basic).

Remarks

If the value parameter implements the IConvertible interface, the method calls the IConvertible.ToString(IFormatProvider) implementation of value. Otherwise, if the value parameter implements the IFormattable interface, the method calls its IFormattable.ToString(String, IFormatProvider) implementation. If value implements neither interface, the method calls the value parameter's ToString() method.

The provider parameter is used if the value parameter implements the IConvertible or IFormattable interface. The provider parameter specifies culture-specific information used in the conversion of value. For example, if the value parameter is a negative decimal number, the provider parameter can supply culture-specific information about the notation used for the negative sign and decimal separator.

Examples

The following code example converts a non-numeric Object and a boxed numeric value to String s with the ToString method, using an IFormatProvider object that displays the type of the format provider for which it is called. The example shows that for the non-numeric object, the IFormatProvider object is not referenced.

' Example of Convert.ToString( non-numeric types, IFormatProvider ).
Imports System.Globalization

' An instance of this class can be passed to methods that require 
' an IFormatProvider.
Public Class DummyProvider
   Implements IFormatProvider

   ' Normally, GetFormat returns an object of the requested type
   ' (usually itself) if it is able; otherwise, it returns Nothing. 
   Public Function GetFormat(ByVal argType As Type) As Object _
       Implements IFormatProvider.GetFormat

      ' Here, the type of argType is displayed, and GetFormat
      ' always returns Nothing.
      outputBlock.Text &= String.Format("{0,-40}", argType.ToString())
      Return Nothing

   End Function
End Class

Module Example

   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

      ' Create an instance of the IFormatProvider.
      Dim provider As New DummyProvider()
      Dim converted As String

      ' Convert these values using DummyProvider.
      Dim Int32A As Integer = -252645135
      Dim DoubleA As Double = 61680.3855
      Dim ObjDouble As Object = CType(-98765.4321, Object)
      Dim DayTimeA As DateTime = _
                          New DateTime(2001, 9, 11, 13, 45, 0)

      Dim BoolA As Boolean = True
      Dim StringA As String = "Qwerty"
      Dim CharA As Char = "$"c
      Dim TSpanA As TimeSpan = New TimeSpan(0, 18, 0)
      Dim ObjOther As Object = CType(provider, Object)

      outputBlock.Text &= "This example of " & _
          "Convert.ToString( non-numeric, IFormatProvider ) " & _
          vbCrLf & "generates the following output. The " & _
          "provider type, argument type, " & vbCrLf & "and " & _
          "argument value are displayed." & vbCrLf
      outputBlock.Text &= vbCrLf & _
          "Note: The IFormatProvider object is not called for " & _
          "Boolean, String, " & vbCrLf & "Char, TimeSpan, " & _
          "and non-numeric Object." & vbCrLf

      ' The format provider is called for these conversions.
      outputBlock.Text &= vbCrLf
      converted = Convert.ToString(Int32A, provider)
      outputBlock.Text &= String.Format("Int32    {0}", converted) & vbCrLf
      converted = Convert.ToString(DoubleA, provider)
      outputBlock.Text &= String.Format("Double   {0}", converted) & vbCrLf
      converted = Convert.ToString(ObjDouble, provider)
      outputBlock.Text &= String.Format("Object   {0}", converted) & vbCrLf
      converted = Convert.ToString(DayTimeA, provider)
      outputBlock.Text &= String.Format("DateTime {0}", converted) & vbCrLf

      ' The format provider is not called for these conversions.
      outputBlock.Text &= vbCrLf
      converted = Convert.ToString(BoolA, provider)
      outputBlock.Text &= String.Format("Boolean  {0}", converted) & vbCrLf
      converted = Convert.ToString(StringA, provider)
      outputBlock.Text &= String.Format("String   {0}", converted) & vbCrLf
      converted = Convert.ToString(CharA, provider)
      outputBlock.Text &= String.Format("Char     {0}", converted) & vbCrLf
      converted = Convert.ToString(TSpanA, provider)
      outputBlock.Text &= String.Format("TimeSpan {0}", converted) & vbCrLf
      converted = Convert.ToString(ObjOther, provider)
      outputBlock.Text &= String.Format("Object   {0}", converted) & vbCrLf

   End Sub
End Module

' This example of Convert.ToString( non-numeric, IFormatProvider )
' generates the following output. The provider type, argument type,
' and argument value are displayed.
'
' Note: The IFormatProvider object is not called for Boolean, String,
' Char, TimeSpan, and non-numeric Object.
' 
' System.Globalization.NumberFormatInfo   Int32    -252645135
' System.Globalization.NumberFormatInfo   Double   61680.3855
' System.Globalization.NumberFormatInfo   Object   -98765.4321
' System.Globalization.DateTimeFormatInfo DateTime 9/11/2001 1:45:00 PM
' 
' Boolean  True
' String   Qwerty
' Char     $
' TimeSpan 00:18:00
' Object   DummyProvider
// Example of Convert.ToString( non-numeric types, IFormatProvider ).
using System;
using System.Globalization;

// An instance of this class can be passed to methods that require 
// an IFormatProvider.
public class DummyProvider : IFormatProvider
{
   // Normally, GetFormat returns an object of the requested type
   // (usually itself) if it is able; otherwise, it returns Nothing. 
   public object GetFormat(Type argType)
   {
      // Here, the type of argType is displayed, and GetFormat
      // always returns Nothing.
      outputBlock.Text += String.Format("{0,-40}", argType.ToString());
      return null;
   }
}

class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      // Create an instance of the IFormatProvider.
      DummyProvider provider = new DummyProvider();
      string converted;

      // Convert these values using DummyProvider.
      int Int32A = -252645135;
      double DoubleA = 61680.3855;
      object ObjDouble = (object)(-98765.4321);
      DateTime DayTimeA = new DateTime(2001, 9, 11, 13, 45, 0);

      bool BoolA = true;
      string StringA = "Qwerty";
      char CharA = '$';
      TimeSpan TSpanA = new TimeSpan(0, 18, 0);
      object ObjOther = (object)provider;

      outputBlock.Text += "This example of " +
          "Convert.ToString( non-numeric, IFormatProvider ) \n" +
          "generates the following output. The provider type, " +
          "argument type, \nand argument value are displayed." + "\n";
      outputBlock.Text += "\nNote: The IFormatProvider object is " +
          "not called for Boolean, String, \nChar, TimeSpan, " +
          "and non-numeric Object." + "\n";

      // The format provider is called for these conversions.
      outputBlock.Text += "\n";
      converted = Convert.ToString(Int32A, provider);
      outputBlock.Text += String.Format("int      {0}", converted) + "\n";
      converted = Convert.ToString(DoubleA, provider);
      outputBlock.Text += String.Format("double   {0}", converted) + "\n";
      converted = Convert.ToString(ObjDouble, provider);
      outputBlock.Text += String.Format("object   {0}", converted) + "\n";
      converted = Convert.ToString(DayTimeA, provider);
      outputBlock.Text += String.Format("DateTime {0}", converted) + "\n";

      // The format provider is not called for these conversions.
      outputBlock.Text += "\n";
      converted = Convert.ToString(BoolA, provider);
      outputBlock.Text += String.Format("bool     {0}", converted) + "\n";
      converted = Convert.ToString(StringA, provider);
      outputBlock.Text += String.Format("string   {0}", converted) + "\n";
      converted = Convert.ToString(CharA, provider);
      outputBlock.Text += String.Format("char     {0}", converted) + "\n";
      converted = Convert.ToString(TSpanA, provider);
      outputBlock.Text += String.Format("TimeSpan {0}", converted) + "\n";
      converted = Convert.ToString(ObjOther, provider);
      outputBlock.Text += String.Format("object   {0}", converted) + "\n";
   }
}

/*
This example of Convert.ToString( non-numeric, IFormatProvider )
generates the following output. The provider type, argument type,
and argument value are displayed.

Note: The IFormatProvider object is not called for Boolean, String,
Char, TimeSpan, and non-numeric Object.

System.Globalization.NumberFormatInfo   int      -252645135
System.Globalization.NumberFormatInfo   double   61680.3855
System.Globalization.NumberFormatInfo   object   -98765.4321
System.Globalization.DateTimeFormatInfo DateTime 9/11/2001 1:45:00 PM

bool     True
string   Qwerty
char     $
TimeSpan 00:18:00
object   DummyProvider

*/

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.