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

Csharp

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
raja
Published on 09-Aug-2018 11:45:52
Advertisements