Decrease and increase

I am new to C # (and OOP). When I have 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 Manager , but when I do it as Employee , does it mean that I upgrade it?

Question 2 :

When I have several objects of the Employee class, and some, but not all of them, are Manager , how can I downplay them, if possible?

+61
c # oop downcasting upcasting
06 Oct '09 at 8:03
source share
4 answers
  • It is right. When you do this, you throw it into the "employee" object, so that means that you cannot access any specific managers.

  • Downcasting is the place where you take the base class and then try to turn it into a more specific class. This can be accomplished 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 as follows:

 Manager m = (employee as Manager); if (m != null) { //do something with it } 

If something is unclear, I will be happy to fix it!

+64
Oct 06 '09 at 8:09
source share

Speeding up (using (Employee)someInstance ) is usually easy, as the compiler can tell you at compile time if the type is derived from another.

Downcasting , however, should be performed at runtime, as a rule, since the compiler may not always know if a given instance is of the specified type. C # provides two operators for this - - , which tells you whether downcast is working and returns true / false. And like , that tries to perform a cast and returns the correct type if possible, or null if not.

To check 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"); 
+39
Oct 06 '09 at 8:10
source share

If you need to check each of the Employee objects, whether it is a manager object, use OfType method:

 List<Employee> employees = new List<Employee>(); //Code to add some Employee or Manager objects.. var onlyManagers = employees.OfType<Manager>(); foreach (Manager m in onlyManagers) { // Do Manager specific thing.. } 
+4
Jan 22 '14 at 18:05
source share
  • Upcasting is an operation that creates a base class link from a subclass link. (subclass → superclass) (i.e. Manager → Employee)
  • downcasting is an operation that creates a subclass link from a base class link. (superclass → subclass) (i.e. Employee → Manager)

In your case

 Employee emp = (Employee)mgr; //mgr is Manager 

you make a raise.

Unlike downcast, which requires an explicit cast, it always succeeds because it could potentially crash at runtime. ( InvalidCastException ).

C # offers two statements to eliminate this exception:

Beginning with:

 Employee e = new Employee(); 

At first:

 Manager m = e as Manager; // if downcast fails m is null; no exception thrown 

Secondly:

 if (e is Manager){...} // the predicate is false if the downcast is not possible 

Warnign . When you do an upcast, you can only access methods, properties, etc. superclass ...

+3
Sep 28 '15 at 13:48
source share



All Articles