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
Updated on: 2020-06-20T15:59:33+05:30

474 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements