Here we have a base class called “BaseClass” and 2 children class (ChildA and ChildB) which have a function in common and another class specific function.
Now let’s imagine that we have a controller that has a field which references an object of type BaseClass, but we want to be able to access the children specific functions of ChildA and ChildB. The problem is here is that, by default, C# will not be able to cast the children automatically: you would have to be able to get the type information of the element, and then perform casting.
Performing type comparison like this can be a tricky business and should usually be avoided. Thankfully, C# is actually capable of performing this automated polymorphism, it’s just that this functionality is hidden! You only need to execute a switch statement on your object to perform this task.
This solution needs you to write a little dispatch function but that’s no big deal. Let’s see how our controller would look like: