Convert.ToChar メソッド

指定した値を Unicode 文字に変換します。

オーバーロードの一覧

このメソッドを呼び出すと、必ず 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;

指定した 8 ビット符号なし整数の値を等価の Unicode 文字に変換します。

.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;

指定した Unicode 文字を返します。実際の変換処理は実行されません。

.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;

このメソッドを呼び出すと、必ず 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;

このメソッドを呼び出すと、必ず InvalidCastException がスローされます。

.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;

このメソッドを呼び出すと、必ず InvalidCastException がスローされます。

.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;

指定した 16 ビット符号付き整数の値を等価の Unicode 文字に変換します。

.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;

指定した 32 ビット符号付き整数の値を等価の Unicode 文字に変換します。

.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;

指定した 64 ビット符号付き整数の値を等価の Unicode 文字に変換します。

.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;

指定した Object の値を Unicode 文字に変換します。

.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;

指定した 8 ビット符号付き整数の値を等価の Unicode 文字に変換します。このメソッドは、CLS と互換性がありません。

.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;

このメソッドを呼び出すと、必ず InvalidCastException がスローされます。

.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;

String の 1 番目の文字を Unicode 文字に変換します。

.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;

指定した 16 ビット符号なし整数の値を等価の Unicode 文字に変換します。このメソッドは、CLS と互換性がありません。

.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;

指定した 32 ビット符号なし整数の値を等価の Unicode 文字に変換します。このメソッドは、CLS と互換性がありません。

.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;

指定した 64 ビット符号なし整数の値を等価の Unicode 文字に変換します。このメソッドは、CLS と互換性がありません。

.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;

指定したカルチャに固有の書式情報を使用して、指定した Object の値を等価の Unicode 文字に変換します。

.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;

指定したカルチャに固有の書式情報を使用して、 String の 1 番目の文字を Unicode 文字に変換します。

.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;

使用例

[Visual Basic, C#, C++] 目的の書式プロバイダの型を表示する IFormatProvider オブジェクトを使用し、 String 形式の Char の値を ToChar メソッドで変換するコード例を次に示します。書式プロバイダが参照されないことを示す例です。

[Visual Basic, C#, C++] メモ   ここでは、ToChar のオーバーロード形式のうちの 1 つだけについて、使用例を示します。その他の例については、各オーバーロード形式のトピックを参照してください。

 
' 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] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

参照

Convert クラス | Convert メンバ | System 名前空間