What is the difference between type conversion and type casting in C#?


Type conversion and type casting are the same in C#. It is converting one type of data to another type. In C#, type casting has two forms −

  • Implicit type conversion − These conversions are 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 − These conversions are done explicitly by users using the pre-defined functions. Explicit conversions require a cast operator.

The following is an example showing how to cast double to int −

Example

 Live Demo

using System;
namespace Demo {
   class Program {
      static void Main(string[] args) {
         double d = 9322.46;
         int i;

         // cast double to int
         i = (int)d;
         Console.WriteLine(i);
         Console.ReadKey();
      }
   }
}

Output

9322

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 20-Jun-2020

304 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements