Export (0) Print
Expand All

Convert.ChangeType Method (Object, TypeCode)

Returns an object of the specified type whose value is equivalent to the specified object.

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

public static Object ChangeType(
	Object value,
	TypeCode typeCode
)

Parameters

value
Type: System.Object

An object that implements the IConvertible interface.

typeCode
Type: System.TypeCode

The type of object to return.

Return Value

Type: System.Object
An object whose underlying type is typeCode and whose value is equivalent to value.
-or-
A null reference (Nothing in Visual Basic), if value is null and typeCode is Empty, String, or Object.

ExceptionCondition
InvalidCastException

This conversion is not supported.

-or-

value is null and typeCode specifies a value type.

-or-

value does not implement the IConvertible interface.

FormatException

value is not in a format recognized by the typeCode type.

OverflowException

value represents a number that is out of the range of the typeCode type.

ArgumentException

typeCode is invalid.

ChangeType(Object, TypeCode) is a general-purpose conversion method that converts the object specified by value to a predefined type specified by typeCode. The value parameter can be an object of any type. For the conversion to succeed, value must implement the IConvertible interface, because the method simply wraps a call to an appropriate IConvertible method. The method also requires that conversion of value to typeCode be supported.

The ChangeType(Object, TypeCode) method does not support the conversion of value to a custom type. To perform such a conversion, call the ChangeType(Object, Type) method.

The following example illustrates how to use the ChangeType(Object, TypeCode) method to change an Object to the type specified by the TypeCode parameter, if possible.

using System;

public class ChangeTypeTest {
    public static void Main() {

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

        Console.WriteLine("The Double {0} when converted to an Int32 is {1}", d, i);

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

        Console.WriteLine("The String {0} when converted to a Date is {1}", s, dt);        
    }
}
// The example displays the following output: 
//    The Double -2.345 when converted to an Int32 is -2 
//    The String 12/12/2009 when converted to a Date is 12/12/2009 12:00:00 AM

.NET Framework

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

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Show:
© 2014 Microsoft