What is Type conversion in C#?

CsharpProgrammingServer Side Programming

To convert one datatype to another in C#, use Type Conversion. In C#, the type conversions are of two types −

Implicit type conversion

Performed by C# in a type-safe manner. For example, are conversions from smaller to larger integral types and conversions from derived classes to base classes.

Explicit type conversion

Performed explicitly by users using the pre-defined functions. Explicit conversions require a cast operator.

Let us see an example to cast double to int i.e. explicit type conversion −

Example

 Live Demo

using System;

namespace Demo {
   class Program {

      static void Main(string[] args) {
         double d = 8745.97;
         int x;
         x = (int)d;
         Console.WriteLine(x);
         Console.ReadKey();
      }
   }
}

Output

8745
raja
Published on 10-Aug-2018 15:56:49
Advertisements