Convert.ChangeType Method in C#


The ChangeType() method returns an object of the specified type and whose value is equivalent to the specified object.

Let’s say we have a double type.

double val = -3.456

Now, use the ChangeType method to change the type to integer.

num = (int)Convert.ChangeType(val, TypeCode.Int32);

Let us see the complete example.

Example

 Live Demo

using System;
public class Demo {
   public static void Main() {
      double val = -3.456;
      int num = (int)Convert.ChangeType(val, TypeCode.Int32);
      Console.WriteLine("{0} converted to an Int32: {1}", val, num);
   }
}

Output

-3.456 converted to an Int32: -3

Updated on: 23-Jun-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements