Please enable Javascript to correctly display the contents on Dot Net Tricks!

Understanding Type Casting or Type Conversion in C#

  Author : Shailendra Chauhan
Posted On : 12 Oct 2013
Total Views : 112,790   
Updated On : 12 Oct 2013
 

Type Casting or Type Conversion is a mechanism to convert one data type value to another one. Type conversion is possible if both the data types are compatible to each other; otherwise you will get an InvalidCastException.

Different Types of Type Casting or Type Conversion

  1. Implicit conversion

    Implicit conversion is being done automatically by the compiler and no data will be lost. It includes conversion of a smaller data type to a larger data types and conversion of derived classes to base class. This is a safe type conversion.

    int smallnum = 654667;
    // Implicit conversion
    long bigNum = smallnum;
    
    class Base
    {
     public int num1 { get; set; }
    }
    
    class Derived : Base
    {
     public int num2 { get; set; }
    }
    
    class Program
    {
     static void Main(string[] args)
     {
     Derived d = new Derived();
     //Implicit Conversion
     Base b = d;
     }
    }
    
  2. Explicit conversion

    Explicit conversion is being done by using a cast operator. It includes conversion of larger data type to smaller data type and conversion of base class to derived classes. In this conversion information might be lost or conversion might not be succeed for some reasons. This is an un-safe type conversion.

    long bigNum = 654667;
    // Explicit conversion
    int smallnum = (int)bigNum;
    
    class Base
    {
     public int num1 { get; set; }
    }
    
    class Derived : Base
    {
     public int num2 { get; set; }
    }
    
    class Program
    {
     static void Main(string[] args)
     {
     Base b = new Base();
     //Explicit Conversion
     Derived d = (Derived)b; 
     }
    }
    
  3. User-defined conversion

    User-defined conversion is performed by using special methods that you can define to enable explicit and implicit conversions. It includes conversion of class to struct or basic data type and struct to class or basic data type. Also, all conversions methods must be declared as static.

    class RationalNumber
    {
     int numerator;
     int denominator;
    
     public RationalNumber(int num, int den)
     {
     numerator = num;
     denominator = den;
     }
    
     public static implicit operator RationalNumber(int i)
     {
     // Rational Number equivalant of an int type has 1 as denominator
     RationalNumber rationalnum = new RationalNumber(i, 1);
     return rationalnum;
     }
    
     public static explicit operator float(RationalNumber r)
     {
     float result = ((float)r.numerator) / r.denominator;
     return result;
     }
    
    }
    class Program
    {
     static void Main(string[] args)
     {
     // Implicit Conversion from int to rational number 
     RationalNumber rational1 = 23;
    
     //Explicit Conversion from rational number to float
     RationalNumber rational2 = new RationalNumber(3, 2);
     float d = (float)rational2;
     }
    }
    

What is the Upcasting and Downcasting?

There are two more casting terms Upcasting and Downcasting. basically these are parts of Implicit conversion and Explicit conversion.

Implicit conversion of derived classes to base class is called Upcasting and Explicit conversion of base class to derived classes is called Downcasting.

class Base
{
 public int num1 { get; set; }
}

class Derived : Base
{
 public int num2 { get; set; }
}

class Program
{
 static void Main(string[] args)
 {
 Derived d1 = new Derived();
 //Upcasting
 Base b1 = d1;

 Base b2 = new Base();
 //Downcasting
 Derived d2 = (Derived)b2; 
 }
}
What do you think?

I hope you will enjoy the tips while programming with C#. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.



Free Interview Books
 
COMMENTS (0)
3 AUG
ASP.NET Core with Angular4 Development (Online)

07:00 AM - 9:00 AM IST(+5:30 GMT)

Know More
29 JUL
Angular2 and Angular4 Development (Online)

08:00 AM - 10:00 AM IST(+5.30 GMT)

Know More
29 JUL
ASP.NET MVC with Angular4 Development (Classroom)

09:30 AM - 11:00 AM IST (+5:30GMT)

Know More
29 JUL
Xamarin Forms : Build Cross-platform Apps (Classroom)

09:00 AM - 11:00 AM IST(+5:30 GMT)

Know More
26 JUL
ASP.NET MVC with Angular4 Development (Online)

07:00 AM - 09:00 AM IST(+5.30 GMT)

Know More
22 JUL
MEAN Stack 2 Development (Classroom)

04:00 PM - 05:30 PM IST(+5:30GMT)

20 JUL
Hadoop Development cum Administration (Online)

07:00 AM - 09:00 AM IST(+5:30 GMT)

17 JUL
NodeJS with Angular4 Development (Online)

08:00 PM - 09:30 PM IST(+5.30 GMT)

15 JUL
ASP.NET MVC with Angular4 Development (Online)

03:30 PM - 05:30 PM IST(+5.30 GMT)

1 JUL
ASP.NET MVC with Angular4 Development (Classroom)

08:00 AM - 09:30 AM IST

1 JUL
MEAN Stack 2 Development (Classroom)

11:00 AM - 12:30 PM IST(+5:30 GMT)

30 JUN
Xamarin Forms : Build Cross-platform Apps (Online)

09:30 PM - 11:00PM IST(+5:30GMT)

6 JUN
ASP.NET MVC with Angular4 Development (Online)

09:00 PM-11:00 PM IST(+5.30 GMT)

20 MAY
NodeJS with Angular4 Development (Classroom)

05:00 PM - 07:00 PM

2 MAY
ASP.NET Core Development (Online)

07:00 AM - 09:00 AM IST(+5.30 GMT)

BROWSE BY CATEGORY
 
 
LIKE US ON FACEBOOK
 
+