Search code examples
c#programming-languages

How does down casting quite work? C#


If I instantiate an object like such :

Person hello = new Person();

I'm allowed to do something like

Employee test = (Employee)hello;

What does this do? What'd downcasting all about? So when we allocate memory for the base, and down cast what happens there?


Solution

  • It will not work. It will throw InvalidCastException at runtime. (assuming Employee : Person).