Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
ToChar Method (String, IFormatProvider)

Convert.ToChar Method (String, IFormatProvider)

Converts the first character of a String to a Unicode character using specified culture-specific formatting information.

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

public static char ToChar (
	string value,
	IFormatProvider provider
)
public static char ToChar (
	String value, 
	IFormatProvider provider
)
public static function ToChar (
	value : String, 
	provider : IFormatProvider
) : char

Parameters

value

A String of length 1 or a null reference (Nothing in Visual Basic).

provider

(Reserved) An IFormatProvider interface implementation that supplies culture-specific formatting information.

Return Value

The Unicode character equivalent to the first and only character in value.

Exception typeCondition

ArgumentNullException

value is a null reference (Nothing in Visual Basic).

FormatException

The length of value is not 1.

value must be a null reference (Nothing in Visual Basic) or a String containing a single character.

provider is ignored; it does not participate in this operation.

The following code example converts a String representation of a Char value with the ToChar method, using an IFormatProvider object that displays the type of the format provider for which it is called. The example shows that the format provider is not referenced.

// Example of selected Convert.ToXXX( String, IFormatProvider ) methods.
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)
    {
        // Here, GetFormat displays the name of argType, after removing 
        // the namespace information. GetFormat always returns null.
        string argStr = argType.ToString( );
        if( argStr == "" ) 
            argStr = "Empty";
        argStr = argStr.Substring( argStr.LastIndexOf( '.' ) + 1 );

        Console.Write( "{0,-20}", argStr );
        return null;
    }
}

class ConvertNonNumericProviderDemo
{
    public static void Main( )
    {
        // Create an instance of IFormatProvider.
        DummyProvider provider = new DummyProvider( );
        string format   = "{0,-17}{1,-17}{2}";

        // Convert these values using DummyProvider.
        string Int32A   = "-252645135";
        string DoubleA  = "61680.3855";
        string DayTimeA = "2001/9/11 13:45";

        string BoolA    = "True";
        string StringA  = "Qwerty";
        string CharA    = "$";

        Console.WriteLine( "This example of selected " +
            "Convert.ToXXX( String, IFormatProvider ) \nmethods " +
            "generates the following output. The example displays " +
            "the \nprovider type if the IFormatProvider is called." );
        Console.WriteLine( "\nNote: For the " +
            "ToBoolean, ToString, and ToChar methods, the \n" +
            "IFormatProvider object is not referenced." );

        // The format provider is called for the following conversions.
        Console.WriteLine( );
        Console.WriteLine( format, "ToInt32", Int32A, 
            Convert.ToInt32( Int32A, provider ) );
        Console.WriteLine( format, "ToDouble", DoubleA, 
            Convert.ToDouble( DoubleA, provider ) );
        Console.WriteLine( format, "ToDateTime", DayTimeA, 
            Convert.ToDateTime( DayTimeA, provider ) );

        // The format provider is not called for these conversions.
        Console.WriteLine( );
        Console.WriteLine( format, "ToBoolean", BoolA, 
            Convert.ToBoolean( BoolA, provider ) );
        Console.WriteLine( format, "ToString", StringA, 
            Convert.ToString( StringA, provider ) );
        Console.WriteLine( format, "ToChar", CharA, 
            Convert.ToChar( CharA, provider ) );
    }
}

/*
This example of selected Convert.ToXXX( String, IFormatProvider )
methods generates the following output. The example displays the
provider type if the IFormatProvider is called.

Note: For the ToBoolean, ToString, and ToChar methods, the
IFormatProvider object is not referenced.

NumberFormatInfo    ToInt32          -252645135       -252645135
NumberFormatInfo    ToDouble         61680.3855       61680.3855
DateTimeFormatInfo  ToDateTime       2001/9/11 13:45  9/11/2001 1:45:00 PM

ToBoolean        True             True
ToString         Qwerty           Qwerty
ToChar           $                $
*/ 

// Example of selected Convert.ToXXX( String, IFormatProvider ) methods.
import System.* ;
import System.Globalization.* ;

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 Object GetFormat(Type argType)
    {
        // Here, GetFormat displays the name of argType, after removing 
        // the namespace information. GetFormat always returns null.
        String argStr = argType.ToString();
        if ( argStr.Equals("")) {
            argStr = "Empty";
        }
        argStr = argStr.Substring((argStr.LastIndexOf('.') + 1));

        Console.Write("{0,-20}", argStr);
        return null ;
    } //GetFormat
} //DummyProvider


class ConvertNonNumericProviderDemo
{   
    public static void main(String[] args)
    {
        // Create an instance of IFormatProvider.
        DummyProvider provider =  new DummyProvider();
        String format = "{0,-17}{1,-17}{2}";

        // Convert these values using DummyProvider.
        String int32A = "-252645135";
        String doubleA = "61680.3855";
        String dayTimeA = "2001/9/11 13:45";

        String boolA = "True";
        String stringA = "Qwerty";
        String charA = "$";

        Console.WriteLine(("This example of selected "
            + "Convert.ToXXX( String, IFormatProvider ) \nmethods " 
            + "generates the following output. The example displays " 
            + "the \nprovider type if the IFormatProvider is called."));
        Console.WriteLine(("\nNote: For the " 
            + "ToBoolean, ToString, and ToChar methods, the \n" 
            + "IFormatProvider object is not referenced."));

        // The format provider is called for the following conversions.
        Console.WriteLine();
        Console.WriteLine(format, "ToInt32", int32A,
            System.Convert.ToString(Convert.ToInt32(int32A, provider)));
        Console.WriteLine(format, "ToDouble", doubleA,
            System.Convert.ToString(Convert.ToDouble(doubleA, provider)));
        Console.WriteLine(format, "ToDateTime", dayTimeA, 
            Convert.ToDateTime(dayTimeA, provider));

        // The format provider is not called for these conversions.
        Console.WriteLine();
        Console.WriteLine(format, "ToBoolean", 
        boolA,System.Convert.ToString(Convert.ToBoolean(boolA, provider)));
        Console.WriteLine(format, "ToString", 
        stringA, Convert.ToString(stringA, provider));
        Console.WriteLine(format, "ToChar", charA, 
        System.Convert.ToString(Convert.ToChar(charA, provider)));
    } //main
} //ConvertNonNumericProviderDemo

/*
This example of selected Convert.ToXXX( String, IFormatProvider )
methods generates the following output. The example displays the
provider type if the IFormatProvider is called.

Note: For the ToBoolean, ToString, and ToChar methods, the
IFormatProvider object is not referenced.

NumberFormatInfo    ToInt32          -252645135       -252645135
NumberFormatInfo    ToDouble         61680.3855       61680.3855
DateTimeFormatInfo  ToDateTime       2001/9/11 13:45  9/11/2001 1:45:00 PM

ToBoolean        True             True
ToString         Qwerty           Qwerty
ToChar           $                $
*/

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2015 Microsoft