Up-casting and Down-casting in C# Inheritance

(Last Updated On: February 25, 2019)

Answer: Explanation of Up-casting and Down-casting in C# inheritance with program example

C# Up-Casting

Assignment of derived class object to a base class reference in C# inheritance is known as up-casting. As, Up-casting is implicit any explicit typecast is not required.
For example, in below program in main() method assignment of derived class “Circle” object to base class “Shape” reference is up-casting.



C# Down-Casting

Assignment of base class object to derived class object is known as Down-casting. For example, in below c# program example, we have assigned existing base class object to derived class circle object using typecasting.


Why Down-Casting is required in C# programming?

It is possible that derived class has some specialized method. For example, in above derived class Circle, FillCircle() method is specialized and only available to Circle class not in Shape base class.

So, if we have base class reference that is pointing to child class and we want to call specialized method of child class, we cannot call FillCircle() method using base class reference, as it will not be visible to base class.

So, we have to downcast from base to child class, then call specialized method that is FillCircle(); . Same has been depicted in above main() program.


Recommended to read interview question prefer way of  C# down-cast object for inherited class .