Convert.ChangeType Method (Object, Type)
![]() |
---|
The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience. |
Returns an object of the specified type and whose value is equivalent to the specified object.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- value
-
Type:
System.Object
An object that implements the IConvertible interface.
- conversionType
-
Type:
System.Type
The type of object to return.
Return Value
Type: System.ObjectAn object whose type is conversionType and whose value is equivalent to value.
-or-
A null reference (Nothing in Visual Basic), if value is null and conversionType is not a value type.
Exception | Condition |
---|---|
InvalidCastException | This conversion is not supported. -or- value is null and conversionType is a value type. -or- value does not implement the IConvertible interface. |
FormatException | value is not in a format recognized by conversionType. |
OverflowException | value represents a number that is out of the range of conversionType. |
ArgumentNullException | conversionType is null. |
ChangeType is a general-purpose conversion method that converts the object specified by value to conversionType. The value parameter can be an object of any type, and conversionType can also be a Type object that represents any base or custom 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 requires that conversion of value to conversionType be supported.
This method uses the current thread's culture for the conversion.
Notes to Callers:
The ChangeType(Object, Type) method can convert an enumeration value to another type. However, it cannot convert another type to an enumeration value, even if the source type is the underlying type of the enumeration. To convert a type to an enumeration value, use a casting operator (in C#) or a conversion function (in Visual Basic). The following example illustrates the conversion to and from a Continent enumeration value.
using System; public enum Continent { Africa, Antarctica, Asia, Australia, Europe, NorthAmerica, SouthAmerica }; public class Example { public static void Main() { // Convert a Continent to a Double. Continent cont = Continent.NorthAmerica; Console.WriteLine("{0:N2}", Convert.ChangeType(cont, typeof(Double))); // Convert a Double to a Continent. Double number = 6.0; try { Console.WriteLine("{0}", Convert.ChangeType(number, typeof(Continent))); } catch (InvalidCastException) { Console.WriteLine("Cannot convert a Double to a Continent"); } Console.WriteLine("{0}", (Continent) number); } } // The example displays the following output: // 5.00 // Cannot convert a Double to a Continent // SouthAmerica
The ChangeType(Object, Type) method can convert a nullable type to another type. However, it cannot convert another type to a value of a nullable type, even if conversionType is the underlying type of the Nullable<T>.To perform the conversion, you can use a casting operator (in C#) or a conversion function (in Visual Basic). The following example illustrates the conversion to and from a nullable type.
using System; public class Example { public static void Main() { int? intValue1 = 12893; double dValue1 = (double) Convert.ChangeType(intValue1, typeof(Double)); Console.WriteLine("{0} ({1})--> {2} ({3})", intValue1, intValue1.GetType().Name, dValue1, dValue1.GetType().Name); float fValue1 = 16.3478f; int? intValue2 = (int) fValue1; Console.WriteLine("{0} ({1})--> {2} ({3})", fValue1, fValue1.GetType().Name, intValue2, intValue2.GetType().Name); } } // The example displays the following output: // 12893 (Int32)--> 12893 (Double) // 16.3478 (Single)--> 16 (Int32)
The following example illustrates the use of the ChangeType method.
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); } }
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Windows Phone Silverlight
Available since 8.0
Windows Phone
Available since 8.1