Convert.ChangeTypeとは? わかりやすく解説

Convert.ChangeType メソッド (Object, TypeCode, IFormatProvider)

指定した TypeCode で、指定したオブジェクトに等しい値を持つ Object を返します。パラメータにより、カルチャに固有の書式情報が指定されます。

名前空間: System
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Shared Function ChangeType
 ( _
    value As Object, _
    typeCode As TypeCode, _
    provider As IFormatProvider _
) As Object
public static Object ChangeType (
    Object value,
    TypeCode typeCode,
    IFormatProvider provider
)
public:
static Object^ ChangeType (
    Object^ value, 
    TypeCode typeCode, 
    IFormatProvider^ provider
)
public static Object ChangeType (
    Object value, 
    TypeCode typeCode, 
    IFormatProvider provider
)
public static function ChangeType
 (
    value : Object, 
    typeCode : TypeCode, 
    provider : IFormatProvider
) : Object

パラメータ

value

IConvertible インターフェイスを実装する Object。

typeCode

TypeCode。

provider

カルチャに固有の書式情報を提供する IFormatProvider インターフェイス実装。

戻り値
基になる TypeCode が typeCode であり、value と等価の値を持つオブジェクト。 または value が null 参照 (Visual Basic では Nothing) に等しく、typeCode が Empty、String、または Object に等しい場合は null 参照 (Visual Basic では Nothing)。

例外例外
例外の種類条件

InvalidCastException

この変換はサポートされていません。

または

value が null 参照 (Visual Basic では Nothing) であり、typeCode が値型を指定しています。

ArgumentException

typeCode が無効です。

解説解説

provider を使用すると、value の内容について、カルチャに固有の変換情報を指定できます。たとえば、value が数値を表す String の場合、provider によって、その数値の表記方法に関するカルチャに固有の情報を提供できます。

使用例使用例

ChangeType メソッドの使用方法を次のコード例に示します。

Imports System

Public Class ChangeTypeTest
    
    Public Shared Sub Main()
        Dim d As [Double] = - 2.345
        Dim i As Integer
 = CInt(Convert.ChangeType(d, GetType(Integer)))
        
        Console.WriteLine("The double value {0} when converted
 to an int becomes {1}", d, i)
        Dim s As String
 = "12/12/98"
        Dim dt As DateTime = CType(Convert.ChangeType(s,
 GetType(DateTime)), DateTime)
        
        Console.WriteLine("The string value {0} when converted
 to a Date becomes {1}", s, dt)
    End Sub 'Main
End Class 'ChangeTypeTest
using System;

public class ChangeTypeTest {
    public static void Main()
 {

        Double d = -2.345;
        int i = (int)Convert.ChangeType(d,
 typeof(int));

        Console.WriteLine("The double value {0} when converted to an int
 becomes {1}", d, i);

        string s = "12/12/98";
        DateTime dt = (DateTime)Convert.ChangeType(s, typeof(DateTime));

        Console.WriteLine("The string value {0} when converted
 to a Date becomes {1}", s, dt);        
    }
}
using namespace System;

int main()
{
   Double d = -2.345;
   int i =  *safe_cast<Int32^>(Convert::ChangeType( d, int::typeid
 ));
   Console::WriteLine( "The double value {0} when converted to an int
 becomes {1}", d, i );
   String^ s = "12/12/98";
   DateTime dt =  *safe_cast<DateTime^>(Convert::ChangeType( s, DateTime::typeid
 ));
   Console::WriteLine( "The string value {0} when converted
 to a Date becomes {1}", s, dt );
}
import System.*;

public class ChangeTypeTest
{
    public static void main(String[]
 args)
    {
        Double d = new Double(-2.345);
        int i = Convert.ToInt32((Convert.ChangeType(d, int.class.ToType())));

        Console.WriteLine("The double value {0} when converted to an "
            + "int becomes {1}", System.Convert.ToString(d)
,
            System.Convert.ToString(i));

        String s = "12/12/98";
        DateTime dt = (DateTime)(Convert.ChangeType(s, DateTime.
            class.ToType()));
        Console.WriteLine("The string value {0} when converted
 to "
            + "a Date becomes {1}", s, dt);
    } //main
} //ChangeTypeTest
import System;

public class ChangeTypeTest {
    public static function
 Main() {

        var d : Double = -2.345;
        var i : Int32 = Int32(Convert.ChangeType(d, Int32));

        Console.WriteLine("The double value {0} when converted to an int
 becomes {1}", d, i);

        var s : String = "12/12/98";
        var dt : DateTime = DateTime(Convert.ChangeType(s, DateTime));

        Console.WriteLine("The string value {0} when converted
 to a Date becomes {1}", s, dt);        
    }
}
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

Convert.ChangeType メソッド (Object, Type)

指定した Type で、指定したオブジェクトに等しい値を持つ Object を返します。

名前空間: System
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Shared Function ChangeType
 ( _
    value As Object, _
    conversionType As Type _
) As Object
Dim value As Object
Dim conversionType As Type
Dim returnValue As Object

returnValue = Convert.ChangeType(value, conversionType)
public static Object ChangeType (
    Object value,
    Type conversionType
)
public:
static Object^ ChangeType (
    Object^ value, 
    Type^ conversionType
)
public static Object ChangeType (
    Object value, 
    Type conversionType
)
public static function ChangeType
 (
    value : Object, 
    conversionType : Type
) : Object

パラメータ

value

IConvertible インターフェイスを実装する Object。

conversionType

Type。

戻り値
Type が conversionType であり、value と等価の値を持つオブジェクト。 または value が null 参照 (Visual Basic では Nothing) で、conversionType が値型ではない場合は、null 参照 (Visual Basic では Nothing)。

例外例外
例外の種類条件

InvalidCastException

この変換はサポートされていません。

または

value が null 参照 (Visual Basic では Nothing) であり、conversionType が値型です。

ArgumentNullException

conversionType が null 参照 (Visual Basic では Nothing) です。

解説解説
使用例使用例

ChangeType メソッドの使用方法を次のコード例に示します。

Imports System

Public Class ChangeTypeTest
    
    Public Shared Sub Main()
        Dim d As [Double] = - 2.345
        Dim i As Integer
 = CInt(Convert.ChangeType(d, GetType(Integer)))
        
        Console.WriteLine("The double value {0} when converted
 to an int becomes {1}", d, i)
        Dim s As String
 = "12/12/98"
        Dim dt As DateTime = CType(Convert.ChangeType(s,
 GetType(DateTime)), DateTime)
        
        Console.WriteLine("The string value {0} when converted
 to a Date becomes {1}", s, dt)
    End Sub 'Main
End Class 'ChangeTypeTest
using System;

public class ChangeTypeTest {
    public static void Main()
 {

        Double d = -2.345;
        int i = (int)Convert.ChangeType(d,
 typeof(int));

        Console.WriteLine("The double value {0} when converted to an int
 becomes {1}", d, i);

        string s = "12/12/98";
        DateTime dt = (DateTime)Convert.ChangeType(s, typeof(DateTime));

        Console.WriteLine("The string value {0} when converted
 to a Date becomes {1}", s, dt);        
    }
}
using namespace System;

int main()
{
   Double d = -2.345;
   int i =  *safe_cast<Int32^>(Convert::ChangeType( d, int::typeid
 ));
   Console::WriteLine( "The double value {0} when converted to an int
 becomes {1}", d, i );
   String^ s = "12/12/98";
   DateTime dt =  *safe_cast<DateTime^>(Convert::ChangeType( s, DateTime::typeid
 ));
   Console::WriteLine( "The string value {0} when converted
 to a Date becomes {1}", s, dt );
}
import System.*;

public class ChangeTypeTest
{
    public static void main(String[]
 args)
    {
        Double d = new Double(-2.345);
        int i = Convert.ToInt32((Convert.ChangeType(d, int.class.ToType())));

        Console.WriteLine("The double value {0} when converted to an "
            + "int becomes {1}", System.Convert.ToString(d)
,
            System.Convert.ToString(i));

        String s = "12/12/98";
        DateTime dt = (DateTime)(Convert.ChangeType(s, DateTime.
            class.ToType()));
        Console.WriteLine("The string value {0} when converted
 to "
            + "a Date becomes {1}", s, dt);
    } //main
} //ChangeTypeTest
import System;

public class ChangeTypeTest {
    public static function
 Main() {

        var d : Double = -2.345;
        var i : Int32 = Int32(Convert.ChangeType(d, Int32));

        Console.WriteLine("The double value {0} when converted to an int
 becomes {1}", d, i);

        var s : String = "12/12/98";
        var dt : DateTime = DateTime(Convert.ChangeType(s, DateTime));

        Console.WriteLine("The string value {0} when converted
 to a Date becomes {1}", s, dt);        
    }
}
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

Convert.ChangeType メソッド (Object, Type, IFormatProvider)

指定した Type で、指定したオブジェクトに等しい値を持つ Object を返します。パラメータにより、カルチャに固有の書式情報が指定されます。

名前空間: System
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Shared Function ChangeType
 ( _
    value As Object, _
    conversionType As Type, _
    provider As IFormatProvider _
) As Object
Dim value As Object
Dim conversionType As Type
Dim provider As IFormatProvider
Dim returnValue As Object

returnValue = Convert.ChangeType(value, conversionType, provider)
public static Object ChangeType (
    Object value,
    Type conversionType,
    IFormatProvider provider
)
public:
static Object^ ChangeType (
    Object^ value, 
    Type^ conversionType, 
    IFormatProvider^ provider
)
public static Object ChangeType (
    Object value, 
    Type conversionType, 
    IFormatProvider provider
)
public static function ChangeType
 (
    value : Object, 
    conversionType : Type, 
    provider : IFormatProvider
) : Object

パラメータ

value

IConvertible インターフェイスを実装する Object。

conversionType

Type。

provider

カルチャに固有の書式情報を提供する IFormatProvider インターフェイス実装。

戻り値
Type が conversionType であり、value と等価の値を持つオブジェクト。 または value の Type と conversionType が等しい場合は value。 または value が null 参照 (Visual Basic では Nothing) で、conversionType が値型ではない場合は、null 参照 (Visual Basic では Nothing)。

例外例外
例外の種類条件

InvalidCastException

この変換はサポートされていません。

または

value が null 参照 (Visual Basic では Nothing) であり、conversionType が値型です。

ArgumentNullException

conversionType が null 参照 (Visual Basic では Nothing) です。

解説解説

provider を使用すると、value の内容について、カルチャに固有の変換情報を指定できます。たとえば、value が数値を表す String の場合、provider によって、その数値の表記方法に関するカルチャに固有の情報を提供できます。

使用例使用例

ChangeType メソッドの使用方法を次のコード例に示します。

Imports System

Public Class ChangeTypeTest
    
    Public Shared Sub Main()
        Dim d As [Double] = - 2.345
        Dim i As Integer
 = CInt(Convert.ChangeType(d, GetType(Integer)))
        
        Console.WriteLine("The double value {0} when converted
 to an int becomes {1}", d, i)
        Dim s As String
 = "12/12/98"
        Dim dt As DateTime = CType(Convert.ChangeType(s,
 GetType(DateTime)), DateTime)
        
        Console.WriteLine("The string value {0} when converted
 to a Date becomes {1}", s, dt)
    End Sub 'Main
End Class 'ChangeTypeTest
using System;

public class ChangeTypeTest {
    public static void Main()
 {

        Double d = -2.345;
        int i = (int)Convert.ChangeType(d,
 typeof(int));

        Console.WriteLine("The double value {0} when converted to an int
 becomes {1}", d, i);

        string s = "12/12/98";
        DateTime dt = (DateTime)Convert.ChangeType(s, typeof(DateTime));

        Console.WriteLine("The string value {0} when converted
 to a Date becomes {1}", s, dt);        
    }
}
using namespace System;

int main()
{
   Double d = -2.345;
   int i =  *safe_cast<Int32^>(Convert::ChangeType( d, int::typeid
 ));
   Console::WriteLine( "The double value {0} when converted to an int
 becomes {1}", d, i );
   String^ s = "12/12/98";
   DateTime dt =  *safe_cast<DateTime^>(Convert::ChangeType( s, DateTime::typeid
 ));
   Console::WriteLine( "The string value {0} when converted
 to a Date becomes {1}", s, dt );
}
import System.*;

public class ChangeTypeTest
{
    public static void main(String[]
 args)
    {
        Double d = new Double(-2.345);
        int i = Convert.ToInt32((Convert.ChangeType(d, int.class.ToType())));

        Console.WriteLine("The double value {0} when converted to an "
            + "int becomes {1}", System.Convert.ToString(d)
,
            System.Convert.ToString(i));

        String s = "12/12/98";
        DateTime dt = (DateTime)(Convert.ChangeType(s, DateTime.
            class.ToType()));
        Console.WriteLine("The string value {0} when converted
 to "
            + "a Date becomes {1}", s, dt);
    } //main
} //ChangeTypeTest
import System;

public class ChangeTypeTest {
    public static function
 Main() {

        var d : Double = -2.345;
        var i : Int32 = Int32(Convert.ChangeType(d, Int32));

        Console.WriteLine("The double value {0} when converted to an int
 becomes {1}", d, i);

        var s : String = "12/12/98";
        var dt : DateTime = DateTime(Convert.ChangeType(s, DateTime));

        Console.WriteLine("The string value {0} when converted
 to a Date becomes {1}", s, dt);        
    }
}
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

Convert.ChangeType メソッド


Convert.ChangeType メソッド (Object, TypeCode)

指定した TypeCode で、指定したオブジェクトに等しい値を持つ Object を返します。

名前空間: System
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

例外例外
例外の種類条件

InvalidCastException

この変換はサポートされていません。

または

value が null 参照 (Visual Basic では Nothing) であり、typeCode が値型を指定しています。

ArgumentException

typeCode が無効です。

使用例使用例

ChangeType メソッドの使用方法を次のコード例に示します。この例では、可能であれば、渡された Object を TypeCode パラメータで指定した型に変更します。

Imports System

Public Class ChangeTypeTest
    
    Public Shared Sub Main()
        Dim d As [Double] = - 2.345
        Dim i As Integer
 = CInt(Convert.ChangeType(d, GetType(Integer)))
        
        Console.WriteLine("The double value {0} when converted
 to an int becomes {1}", d, i)
        Dim s As String
 = "12/12/98"
        Dim dt As DateTime = CType(Convert.ChangeType(s,
 GetType(DateTime)), DateTime)
        
        Console.WriteLine("The string value {0} when converted
 to a Date becomes {1}", s, dt)
    End Sub 'Main
End Class 'ChangeTypeTest
using System;

public class ChangeTypeTest {
    public static void Main()
 {

        Double d = -2.345;
        int i = (int)Convert.ChangeType(d,
 typeof(int));

        Console.WriteLine("The double value {0} when converted to an int
 becomes {1}", d, i);

        string s = "12/12/98";
        DateTime dt = (DateTime)Convert.ChangeType(s, typeof(DateTime));

        Console.WriteLine("The string value {0} when converted
 to a Date becomes {1}", s, dt);        
    }
}
using namespace System;

int main()
{
   Double d = -2.345;
   int i =  *safe_cast<Int32^>(Convert::ChangeType( d, int::typeid
 ));
   Console::WriteLine( "The double value {0} when converted to an int
 becomes {1}", d, i );
   String^ s = "12/12/98";
   DateTime dt =  *safe_cast<DateTime^>(Convert::ChangeType( s, DateTime::typeid
 ));
   Console::WriteLine( "The string value {0} when converted
 to a Date becomes {1}", s, dt );
}
import System.*;

public class ChangeTypeTest
{
    public static void main(String[]
 args)
    {
        Double d = new Double(-2.345);
        int i = Convert.ToInt32((Convert.ChangeType(d, int.class.ToType())));

        Console.WriteLine("The double value {0} when converted to an "
            + "int becomes {1}", System.Convert.ToString(d)
,
            System.Convert.ToString(i));

        String s = "12/12/98";
        DateTime dt = (DateTime)(Convert.ChangeType(s, DateTime.
            class.ToType()));
        Console.WriteLine("The string value {0} when converted
 to "
            + "a Date becomes {1}", s, dt);
    } //main
} //ChangeTypeTest
import System;

public class ChangeTypeTest {
    public static function
 Main() {

        var d : Double = -2.345;
        var i : Int32 = Int32(Convert.ChangeType(d, Int32));

        Console.WriteLine("The double value {0} when converted to an int
 becomes {1}", d, i);

        var s : String = "12/12/98";
        var dt : DateTime = DateTime(Convert.ChangeType(s, DateTime));

        Console.WriteLine("The string value {0} when converted
 to a Date becomes {1}", s, dt);        
    }
}
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



英和和英テキスト翻訳

英語⇒日本語日本語⇒英語

辞書ショートカット

すべての辞書の索引

「Convert.ChangeType」の関連用語

Convert.ChangeTypeのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



Convert.ChangeTypeのページの著作権

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2026 Microsoft.All rights reserved.

©2026 GRAS Group, Inc.RSS