This documentation is archived and is not being maintained.

Convert.ToChar Method

Converts a specified value to a Unicode character.

Overload List

Calling this method always throws InvalidCastException.

[Visual Basic] Overloads Public Shared Function ToChar(Boolean) As Char
[C#] public static char ToChar(bool);
[C++] public: static __wchar_t ToChar(bool);
[JScript] public static function ToChar(Boolean) : Char;

Converts the value of the specified 8-bit unsigned integer to its equivalent Unicode character.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function ToChar(Byte) As Char
[C#] public static char ToChar(byte);
[C++] public: static __wchar_t ToChar(unsigned char);
[JScript] public static function ToChar(Byte) : Char;

Returns the specified Unicode character value; no actual conversion is performed.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function ToChar(Char) As Char
[C#] public static char ToChar(char);
[C++] public: static __wchar_t ToChar(__wchar_t);
[JScript] public static function ToChar(Char) : Char;

Calling this method always throws InvalidCastException.

[Visual Basic] Overloads Public Shared Function ToChar(DateTime) As Char
[C#] public static char ToChar(DateTime);
[C++] public: static __wchar_t ToChar(DateTime);
[JScript] public static function ToChar(DateTime) : Char;

Calling this method always throws InvalidCastException.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function ToChar(Decimal) As Char
[C#] public static char ToChar(decimal);
[C++] public: static __wchar_t ToChar(Decimal);
[JScript] public static function ToChar(Decimal) : Char;

Calling this method always throws InvalidCastException.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function ToChar(Double) As Char
[C#] public static char ToChar(double);
[C++] public: static __wchar_t ToChar(double);
[JScript] public static function ToChar(double) : Char;

Converts the value of the specified 16-bit signed integer to its equivalent Unicode character.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function ToChar(Short) As Char
[C#] public static char ToChar(short);
[C++] public: static __wchar_t ToChar(short);
[JScript] public static function ToChar(Int16) : Char;

Converts the value of the specified 32-bit signed integer to its equivalent Unicode character.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function ToChar(Integer) As Char
[C#] public static char ToChar(int);
[C++] public: static __wchar_t ToChar(int);
[JScript] public static function ToChar(int) : Char;

Converts the value of the specified 64-bit signed integer to its equivalent Unicode character.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function ToChar(Long) As Char
[C#] public static char ToChar(long);
[C++] public: static __wchar_t ToChar(__int64);
[JScript] public static function ToChar(long) : Char;

Converts the value of the specified Object to a Unicode character.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function ToChar(Object) As Char
[C#] public static char ToChar(object);
[C++] public: static __wchar_t ToChar(Object*);
[JScript] public static function ToChar(Object) : Char;

Converts the value of the specified 8-bit signed integer to its equivalent Unicode character. This method is not CLS-compliant.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function ToChar(SByte) As Char
[C#] public static char ToChar(sbyte);
[C++] public: static __wchar_t ToChar(char);
[JScript] public static function ToChar(SByte) : Char;

Calling this method always throws InvalidCastException.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function ToChar(Single) As Char
[C#] public static char ToChar(float);
[C++] public: static __wchar_t ToChar(float);
[JScript] public static function ToChar(float) : Char;

Converts the first character of a String to a Unicode character.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function ToChar(String) As Char
[C#] public static char ToChar(string);
[C++] public: static __wchar_t ToChar(String*);
[JScript] public static function ToChar(String) : Char;

Converts the value of the specified 16-bit unsigned integer to its equivalent Unicode character. This method is not CLS-compliant.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function ToChar(UInt16) As Char
[C#] public static char ToChar(ushort);
[C++] public: static __wchar_t ToChar(unsigned short);
[JScript] public static function ToChar(UInt16) : Char;

Converts the value of the specified 32-bit unsigned integer to its equivalent Unicode character. This method is not CLS-compliant.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function ToChar(UInt32) As Char
[C#] public static char ToChar(uint);
[C++] public: static __wchar_t ToChar(unsigned int);
[JScript] public static function ToChar(UInt32) : Char;

Converts the value of the specified 64-bit unsigned integer to its equivalent Unicode character. This method is not CLS-compliant.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function ToChar(UInt64) As Char
[C#] public static char ToChar(ulong);
[C++] public: static __wchar_t ToChar(unsigned __int64);
[JScript] public static function ToChar(UInt64) : Char;

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

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function ToChar(Object, IFormatProvider) As Char
[C#] public static char ToChar(object, IFormatProvider);
[C++] public: static __wchar_t ToChar(Object*, IFormatProvider*);
[JScript] public static function ToChar(Object, IFormatProvider) : Char;

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

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function ToChar(String, IFormatProvider) As Char
[C#] public static char ToChar(string, IFormatProvider);
[C++] public: static __wchar_t ToChar(String*, IFormatProvider*);
[JScript] public static function ToChar(String, IFormatProvider) : Char;

Example

[Visual Basic, C#, C++] 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.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of ToChar. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
' Example of selected Convert.ToXXX( String, IFormatProvider ) methods.
Imports System
Imports System.Globalization
Imports Microsoft.VisualBasic

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( argType As Type ) As Object _
        Implements IFormatProvider.GetFormat

        ' Here, GetFormat displays the name of argType, after removing 
        ' the namespace information. GetFormat always returns Nothing.
        Dim argStr  As String = argType.ToString( )
        If argStr = "" Then argStr = "Empty"
        argStr = argStr.Substring( argStr.LastIndexOf( "."c ) + 1 )

        Console.Write( "{0,-20}", argStr )
        Return Nothing

    End Function 
End Class

Module ConvertNonNumericProviderDemo

    Sub Main( )

        ' Create an instance of IFormatProvider.
        Dim provider    As New DummyProvider( )
        Dim format      As String   = "{0,-17}{1,-17}{2}"

        ' Convert these values using DummyProvider.
        Dim Int32A      As String   = "-252645135"   
        Dim DoubleA     As String   = "61680.3855"
        Dim DayTimeA    As String   = "2001/9/11 13:45"

        Dim BoolA       As String   = "True"
        Dim StringA     As String   = "Qwerty"
        Dim CharA       As String   = "$"

        Console.WriteLine( "This example of selected " & _
            "Convert.ToXXX( String, IFormatProvider ) " & vbCrLf & _
            "methods generates the following output. The example " & _
            "displays the " & vbCrLf & "provider type if the " & _
            "IFormatProvider is called." )
        Console.WriteLine( vbCrLf & _
            "Note: For the ToBoolean, ToString, and ToChar " & _
            "methods, the " & vbCrLf & "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 ) )

    End Sub
End Module

' 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           $                $

[C#] 
// 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           $                $
*/ 

[C++] 
// Example of selected Convert::ToXXX( String*, IFormatProvider* ) 
// methods.
#using <mscorlib.dll>
using namespace System;
using namespace System::Globalization;

__gc class DummyProvider : public 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 == S"" ) 
            argStr = S"Empty";
        argStr = argStr->Substring( argStr->LastIndexOf( '.' ) + 1 );

        Console::Write( S"{0,-20}", argStr );
        return (Object*)0;
    }
};

void main( )
{
    // Create an instance of IFormatProvider.
    DummyProvider* provider = new DummyProvider( );
    String* format   = S"{0,-17}{1,-17}{2}";

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

    String* BoolA    = S"True";
    String* StringA  = S"Qwerty";
    String* CharA    = S"$";

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

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

    // The format provider is not called for these conversions.
    Console::WriteLine( );
    Console::WriteLine( format, S"ToBoolean", BoolA, 
        __box( Convert::ToBoolean( BoolA, provider ) ) );
    Console::WriteLine( format, S"ToString", StringA, 
        Convert::ToString( StringA, provider ) );
    Console::WriteLine( format, S"ToChar", CharA, 
        __box( 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           $                $
*/ 

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

Convert Class | Convert Members | System Namespace

Show: