What Is Type Casting Or Type Conversion In C#?

In this article, I am going to share with you about Type Casting and its types in C#.
 
What is Type Casting or Type Conversion in C#?
 
Type Conversion is the conversion of one data type into another. Type Conversion is also called Type Casting.
 
In C#, there are two types of Type Conversion -
 
Implicit Type Conversion

Implicit conversion is the conversion when we convert the smaller data type into a larger one and when converting from derived to a base class. As we are converting from smaller data type to larger, there will be no loss of data. It is performed automatically by the compiler. Implicit type conversion of derived to a base class is known as Upcasting. Have a look at the example below.
  1. using System;  
  2.   
  3. namespace Tutpoint  
  4. {  
  5.     class Program  
  6.     {  
  7.         class Base  
  8.         {  
  9.             string Text = "Hello.. Base Class";  
  10.         }  
  11.         class Derived : Base  
  12.         {  
  13.             string Text = "Hello.. Derived Class";  
  14.         }  
  15.         static void Main(string[] args)  
  16.         {  
  17.             // Creating object of Derived class  
  18.             Derived derived = new Derived();  
  19.             // Implicit type as converting from Derived to Base class  
  20.             Base b = derived;  
  21.   
  22.             // A variable 'value_Int' of int type is initialised with value 100  
  23.             int value_Int = 100;  
  24.             // A variable 'value_long' of Type long is assigned with 'value_Int'  
  25.             // This is implicit conversion from smaller value to larger value  
  26.             long value_long = value_Int;  
  27.   
  28.             Console.ReadKey();  
  29.         }  
  30.     }  
  31. }   
Here, we created two classes 'Base' and 'Derived' inside a class 'Program'. The 'Derived' class inherits 'Base' class. As we know in Implicit conversion, we are converting from smaller type to larger one; also, converting from Derived to Base class. Object 'base' of Base class is assigned to the object 'derived' from Derived class. In the second part, we assigned a value of 'int' type to 'long' type. This is all about Implicit Conversion.
 
Explicit type conversion

Explicit conversion is the conversion when we convert the larger data type into a smaller one and when converting from base to derived class. As we are converting from larger data type to smaller, there may be the loss of some part of data. This can be performed by cast operator. Explicit type conversion of the base to the derived class is known as Downcasting. Have a look at the example below.
  1. using System;  
  2.   
  3. namespace Tutpoint  
  4. {  
  5.     class Program  
  6.     {  
  7.         class Base  
  8.         {  
  9.             string Text = "Hello.. Base Class";  
  10.         }  
  11.         class Derived : Base  
  12.         {  
  13.             string Text = "Hello.. Derived Class";  
  14.         }  
  15.         static void Main(string[] args)  
  16.         {  
  17.             // Creating object of Base class  
  18.             Base b = new Base();  
  19.             // Explicit type as converting from Derived to Base class  
  20.             Derived derived = (Derived)b;  
  21.   
  22.             // A variable 'value_Int' of int type is initialised with value 100  
  23.             int value_Int = 100;  
  24.   
  25.             // Here we are trying to convert a larger value into smaller value(explicit conversion) which can not be done direclty.  
  26.             // And produce an error as 'Cannot implicitly convert type 'int' to 'short'. An Explicit conversion exist '  
  27.             short value_short = value_Int;  
  28.   
  29.             // Example of Explicit type as conversion is done from 'Int' type to 'short' type using cast operator.  
  30.             short value_shor = (short)value_Int;  
  31.             Console.ReadKey();  
  32.         }  
  33.     }  
  34. }  
Here, we created two classes 'Base' and 'Derived' inside class 'Program'. 'Derived' class inherits 'Base' class. As we know, in explicit conversion, we are converting from a larger type to smaller one and from Base to Derived class, the object 'derived' from Derived class is assigned to the object 'b' of Base class. In the second part, when we try to convert explicit type directly, an error will occur as "Cannot implicitly convert type 'int' to 'short'. An Explicit conversion exists". On next statement, this is achieved using cast operator. This is all about Explicit Conversion.


Next Recommended Reading Type Casting In C#