Monday, April 16, 2012

Cast base instance to derived class (downcast) in C#

Suppose I have two classes:



class Employee 


and



class AdvancedEmployee:Employee


I know something like this won't work, as I can't downcast on C#:



var employee = new Employee();
var advanced = employee as AdvancedEmployee;


My question is: How to accomplish downcast in a efficient way?
Actually I have a constructor on AdvancedEmployee that takes a Employee as parameter and use it to inject its values, basically making a clone.





No comments:

Post a Comment