Inheritence in C#.Net

Posted on 2005-05-15
Last Modified: 2008-01-09
I have a base class named as MyBaseClass and derived class as MyInheritedClass whihc inherits from MyBaseclass in

In Form1.cs I am clreating an instance of both base and derived as below.

MyInheritedClass MyInherited = new MyInheritedClass() ;

MyBaseClass MyBase ;

what am I doing here by the below syntax..
MyBase = MyInherited;

I understand that using an instance of inherited class I can acccess the members of both base and dervied class.
But I want to know what can I do using the instance MyBase declared above and assigned to MyInherited (a derived class instance)..

Please let me know if I am not clear.

Question by:naveenraj
    LVL 21

    Expert Comment

    if you downcast, from inherited to the base class, you can use function declared in your base class but you can't use function declared in your inherited class.

    I normally use it when I have some custom controls, say custom drop down list, and if I want to implement some simple operations, I would cast my sender directly to ComboBox rather than to my custom control.

    LVL 7

    Expert Comment

    When you do:

    MyBase = MyInherited;

    you are doing an implicit upcast... another way to write this is:

    MyBase = (MyBaseClass)MyInherited;

    so now you have the functionality of MyBaseClass, however in memory is everything for MyInheritedClass.  What this means is that if you then downcase MyBase you have the full functionlity of the original class.  i.e.

    MyInheritedClass mic = (MyInheritedClass) MyBase;


    Author Comment


     I am not clear enough.

    If we are doing as below

    MyBaseClass MyBase  = new MyBaseClass();
    I am creating an instance of base class MyBaseClass and using this instance I can access the members of base class. Similarly for derived class, using the command MyInheritedClass MyInherited = new MyInheritedClass();

    What is the difference between the following 2 types of creating instances.

    Type 1:
    MyInheritedClass MyInherited = new MyInheritedClass() ;
    MyBaseClass MyBase ;            
    MyBase = MyInherited;

    Type 2:
    MyBaseClass MyBase = new MyInheritedClass() ;
    MyInheritedClass MyInherited ;            
    MyInherited = MyBase ;

    I need a basic level of explanation.


    Author Comment

    I didn't get a answer for my above question yet? Please help...

    LVL 7

    Accepted Solution

    The difference is that in your type 1 example you are upcasting, which is done automatically.  However, int your type 2 example you are trying to downcast, which the language specification is that you must explicitly define this cast... i.e. MyInherited = (MyInheritedClass) MyBase;.  So if I were to expand your type 2 example to what is actually happening it would look like this:

    MyBaseClass MyBase = (MyBaseClass) new MyInheritedClass() ;      // Notice the cast to (MyBaseClass) that happens implicitly
    MyInheritedClass MyInherited;
    MyInherited = MyBase;
    // At this point MyBase has all the functionality to all of MyInheritedClass... but it is only allows access to the functionality in MyBaseClass, and in  anycase this statement will cause a compile time error in C#.

    - However -

    MyInherited = (MyInheritedClass) MyBase; // At this point MyBase will cast successfully to MyInheritedClass and will allow access to the full funtionality of MyInheritedClass.


    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
    This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now