C# Convert ChangeType(Object, Type)
Description
Convert ChangeType(Object, Type)
returns an object
of the specified type and whose value is equivalent to the specified object.
Syntax
Convert.ChangeType(Object, Type)
has the following syntax.
public static Object ChangeType(
Object value,
Type conversionType
)
Parameters
Convert.ChangeType(Object, Type)
has the following parameters.
value
- An object that implements the IConvertible interface.conversionType
- The type of object to return.
Returns
Convert.ChangeType(Object, Type)
method returns An 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.
Example
The following example illustrates the conversion to and from a Continent enumeration value.
/*from w w w . ja v a2 s.c om*/
using System;
public enum Continent
{
Africa, Antarctica, Asia, Australia, Europe,
NorthAmerica, SouthAmerica
};
public class Example
{
public static void Main()
{
Continent cont = Continent.NorthAmerica;
Console.WriteLine("{0:N2}",
Convert.ChangeType(cont, typeof(Double)));
}
}
The code above generates the following result.