Difference between Upcasting and Downcasting

Prabhukiran345
Posted by Prabhukiran345 under C# category on | Points: 40 | Views : 811
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.

Example:

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;
}
}

Comments or Responses

Login to post response