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.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version