downcast and upcast
I am new to C# (and OOP). When I have some code like the following:
class Employee
{
// some code
}
class Manager : Employee
{
//some code
}
Question 1: If I have other code that does this:
Manager mgr = new Manager();
Employee emp = (Employee)mgr;
Here Employee
is a Manager
, but when I cast it like that to an Employee
it means I am upcasting it?
Question 2:
When I have several Employee
class objects and some but not all of them are Manager
's, how can I downcast them where possible?
That is correct. When you do that you are casting it it into an
employee
object, so that means you cannot access anything manager specific.-
Downcasting is where you take a base class and then try and turn it into a more specific class. This can be accomplished with using is and an explicit cast like this:
if (employee is Manager) { Manager m = (Manager)employee; //do something with it }
or with the as
operator like this:
Manager m = (employee as Manager);
if (m != null)
{
//do something with it
}
If anything is unclear I'll be happy to correct it!
Upcasting (using (Employee)someInstance
) is generally easy as the compiler can tell you at compile time if a type is derived from another.
Downcasting however has to be done at run time generally as the compiler may not always know whether the instance in question is of the type given. C# provides two operators for this - is which tells you if the downcast works, and return true/false. And as which attempts to do the cast and returns the correct type if possible, or null if not.
To test if an employee is a manager:
Employee m = new Manager();
Employee e = new Employee();
if(m is Manager) Console.WriteLine("m is a manager");
if(e is Manager) Console.WriteLine("e is a manager");
You can also use this
Employee someEmployee = e as Manager;
if(someEmployee != null) Console.WriteLine("someEmployee (e) is a manager");
Employee someEmployee = m as Manager;
if(someEmployee != null) Console.WriteLine("someEmployee (m) is a manager");