Question

Sorry my title is not good as I do not really know how to phrase the question. I saw my seniors using Choice 1 which cast inherited class to base and then cast it back to inherited to use the function of the inherited class.

Why dont they do something like choice 2? Just define the variable as a derived class and no casting is needed.

class question
{
    static void Main()
    {

        #region Choice 1
        baseclass whyusethis = new derivedclass();
        (whyusethis as derivedclass).printbase();
        (whyusethis as derivedclass).printderived();
        #endregion

        #region Choice 2
        derivedclass shouldusethis = new derivedclass();
        shouldusethis.printbase();
        shouldusethis.printderived();
        #endregion

        Console.ReadLine();

    }
}
Was it helpful?

Solution

In a normal inheritance scheme, you always assign to base class objects. You could assign to a derived class reference as "Choice 2" indicates, but you should only do that if you need to call a function that exists only in the derived class (and not the base).

Now, if you are needing to do that, you should be questioning your design anyways, since a users of derived classes should go through the interface which is defined by the base class.

In other words, if the class structure is set up appropriately:

public class BasePrintClass
{
   public void PrintBaseClass()
   {
   }

   public abstract void PrintDerivedClass(); //Why these are seperated only you know
}

public class DerivedPrintClass
{
   public override void PrintDerivedClass()
   {
   }
}

Then you would just use the base class, and let polymorphism handle the derived class call:

BasePrintClass print = new DerivedPrintClass();

print.PrintBaseClass();
print.PrintDerivedClass();

Polymorphism is your friend, use it! The downcasts in "Choice 1" are valid, but not recommended, and not needed if you use the above approach. In "Choice 2" you are avoiding polymorphism entirely by assigning to a derived class reference. This is better than casting, but not necessary if you design it correctly!

To answer the question explicitly, programmers cast to the base class because they have to (need a derived-only function). ANY other time is a sure-fire sign of bad design (or just bad code from people that don't understand polymorphism). Needing a derived-only function is definitely a sign of a questionable design, so feel free to ask about it anyways!

Let me know if I can clarify anything.

OTHER TIPS

It's hard to see any reason in the code you've given - you'd need to give more context. The methods could be explicitly implemented members of an interface if baseclass is actually an interface. It could have had a reason when the code was written which has since been changed.

One thing to note with this code is that the style of cast isn't appropriate here. It's slower than a normal cast, and if, for whatever reason, whyusethis isn't derivedclass, you've traded an InvalidCastException for a confusing NullReferenceException.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top