locked
How to check inheritance in C#? RRS feed

  • Question

  • User881445502 posted

    How would one check that inheritance is correct and class A is indeed a superclass of B?

    Thursday, February 11, 2016 4:03 PM

Answers

All replies

  • User-821857111 posted
    var b = new B();
    Console.WriteLine(b.GetType().BaseType == typeof(A)); //should output True



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 11, 2016 4:36 PM
  • User753101303 posted

    Hi,

    Or maybe https://msdn.microsoft.com/en-us/library/bewds7kc.aspx and check the "is" and "as" keywords depending on what you want to do.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 11, 2016 4:51 PM
  • User303363814 posted

    Whether class A is a superclass of B is a static matter.  That is, you determine it by looking at the source code. 

    Look at the declaration of class B, which class does it inherit from (there can only be one)?  If it is not class A then look to see if that class inherits from another.  If it is not class A then repeat until there is no more inheritance.

    What do you mean by 'inheritance is correct'?  Are you questioning the validity of the design that has been chosen?  Do you want to know, for example, if 'Rectangle' is a good parent class for 'Square'?

    Friday, February 12, 2016 4:51 AM