Convert.ChangeType Method (Object, Type, IFormatProvider)

Returns an object of the specified type whose value is equivalent to the specified object. A parameter supplies culture-specific formatting information.

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

static member ChangeType : 
        value:Object * 
        conversionType:Type * 
        provider:IFormatProvider -> Object

Parameters

value
Type: System.Object

An object that implements the IConvertible interface.

conversionType
Type: System.Type

The type of object to return.

provider
Type: System.IFormatProvider

An object that supplies culture-specific formatting information.

Return Value

Type: System.Object
An object whose type is conversionType and whose value is equivalent to value.
-or-
value, if the Type of value and conversionType are equal.
-or-
A null reference (Nothing in Visual Basic), if value is a null reference (Nothing in Visual Basic) and conversionType is not a value type.

ExceptionCondition
InvalidCastException

This conversion is not supported.

-or-

value is a null reference (Nothing in Visual Basic) and conversionType is a value type.

-or-

value does not implement the IConvertible interface.

FormatException

value is not in a format for conversionType recognized by provider.

OverflowException

value represents a number that is out of the range of conversionType.

ArgumentNullException

conversionType is a null reference (Nothing in Visual Basic).

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.

The provider parameter is an IFormatProvider implementation that supplies formatting information for the conversion. Whether and how this parameter is used depends on the underlying IConvertible implementation. If value is a base data type, provider is used only for the following conversions:

  • Conversion from a number to a string, or from a string to a number. provider must be a CultureInfo object, a NumberFormatInfo object, or a custom IFormatProvider implementation that returns a NumberFormatInfo object. However, because the ChangeType(Object, TypeCode, IFormatProvider) method performs the conversion using the default "G" format specifier, the provider parameter has no effect if value or the target type is an unsigned integer. If provider is a null reference (Nothing in Visual Basic), the CultureInfo object that represents the current thread culture is used.

  • Conversion from a DateTime value to a string, or from a string to a DateTime value. provider must be a CultureInfo or DateTimeFormatInfo object. If provider is a null reference (Nothing in Visual Basic), the CultureInfo object that represents the current thread culture is used.

If value is an application-defined type, its IConvertible implementation may use the provider parameter.

Notes to Callers

The ChangeType(Object, Type, IFormatProvider) 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.

No code example is currently available or this language may not be supported.

The Convert.ChangeType(Object, Type, IFormatProvider) 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.

No code example is currently available or this language may not be supported.

The following example defines a Temperature class that implements the IConvertible interface.

No code example is currently available or this language may not be supported.

The following example creates an instance of the Temperature class and calls the ChangeType(Object, Type, IFormatProvider) method to convert it to the basic numeric types supported by the .NET Framework and to a String. It illustrates that the ChangeType method wraps a call to the source type's IConvertible implementation.

No code example is currently available or this language may not be supported.

.NET Framework

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

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, 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.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft