Export (0) Print
Expand All

Convert.ToString Method (Object, IFormatProvider)

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)

public static string ToString(
	Object value,
	IFormatProvider provider
)

Parameters

value
Type: System.Object

An object that supplies the value to convert, or null.

provider
Type: System.IFormatProvider

An object that supplies culture-specific formatting information.

Return Value

Type: System.String
The string representation of value, or String.Empty if value is null.

If the value parameter implements the IConvertible interface, the return value is produced by the implemented IConvertible.ToString(IFormatProvider) method. Otherwise, if the value parameter implements the IFormattable interface, the return value is produced by the implemented IFormattable.ToString(String, IFormatProvider) method. If value implements neither interface, the return value is produced by 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.

The following example defines a class named DummyProvider that simply intercepts method calls made by the ToString(Object, IFormatProvider) method to the provider parameter's IFormatProvider.GetFormat implementation, and then displays the type name of the object that the IFormatProvider.GetFormat method is expected to return. When the ToString(Object, IFormatProvider) method is passed a number of different data types in an object array, the output indicates whether the method actually uses the provider parameter with that particular data type. The output illustrates that the provider parameter is used when converting numeric and DateTime values to strings, but that it is not used with Boolean, character, string, or TimeSpan values.

using System;
using System.Globalization;

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)
    {
        // Display the type of argType and return null.
        Console.Write( "{0,-25}", argType.Name);
        return null;
    } 
}

public class Example
{
   public static void Main()
   {
      // Create an instance of the IFormatProvider.
      IFormatProvider provider = new DummyProvider();

      // Values to convert using DummyProvider. 
      int int32A = -252645135;   
      double doubleA = 61680.3855;
      object objDouble = (object) -98765.4321;
      DateTime dayTimeA = new DateTime(2009, 9, 11, 13, 45, 0);
      bool boolA = true;
      string stringA = "Qwerty";
      char charA = '$';
      TimeSpan tSpanA = new TimeSpan(0, 18, 0);
      object objOther = provider;

      object[] objects= { int32A, doubleA, objDouble, dayTimeA, 
                          boolA, stringA, charA, tSpanA, objOther };

      // Call Convert.ToString(Object, provider) method for each value. 
      foreach (object value in objects) 
         Console.WriteLine("{0,-20}  -->  {1,20}", 
                           value, Convert.ToString(value, provider));    
   }
}
// The example displays the following output: 
//    NumberFormatInfo         -252645135            -->            -252645135 
//    NumberFormatInfo         61680.3855            -->            61680.3855 
//    NumberFormatInfo         -98765.4321           -->           -98765.4321 
//    DateTimeFormatInfo       9/11/2009 1:45:00 PM  -->  9/11/2009 1:45:00 PM 
//    True                  -->                  True 
//    Qwerty                -->                Qwerty 
//    $                     -->                     $ 
//    00:18:00              -->              00:18:00 
//    DummyProvider         -->         DummyProvider

The following example defines a Temperature class that overrides the Object.ToString method but does not implement the IConvertible interface. The example illustrates how calls to the Convert.ToString(Object, IFormatProvider) method, in turn, call the Temperature.ToString method.

using System;

public class Temperature
{
   private decimal m_Temp;

   public Temperature(decimal temperature)
   {
      this.m_Temp = temperature;
   }

   public decimal Celsius
   {
      get { return this.m_Temp; }   
   }

   public decimal Kelvin
   {
      get { return this.m_Temp + 273.15m; }    
   }

   public decimal Fahrenheit
   {
      get { return Math.Round((decimal) (this.m_Temp * 9 / 5 + 32), 2); }
   }

   public override string ToString()
   {
      return m_Temp.ToString("N2") + " °C";
   }

}

public class Example
{
   public static void Main()
   {
      Temperature cold = new Temperature(-40);
      Temperature freezing = new Temperature(0);
      Temperature boiling = new Temperature(100);

      Console.WriteLine(Convert.ToString(cold, null));
      Console.WriteLine(Convert.ToString(freezing, null));
      Console.WriteLine(Convert.ToString(boiling, null));
   }
}
// The example dosplays the following output: 
//       -40.00 °C 
//       0.00 °C 
//       100.00 °C

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

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

.NET Framework

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

.NET Compact Framework

Supported in: 3.5, 2.0

XNA Framework

Supported in: 3.0, 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft