I do understand that there's no multiple inheritence in C#. However, I've run into a situation in which I really wish it existed. I am creating a custom class that requires me to inherit from CLR types and override a few methods. Unfortunately, I am creating several of these which are very similar. In the interest of DRY, I'd really want to move common functionality to a base class, but then I'd need to inherit from 2 classes. I can use interfaces (and infact I am using one right now) but this solves only half the problem as the method implementations still need to be repeated across several custom classes.
What's the purist way of achieving what I am trying to do?
EDIT:
Here's a generic code sample
public class CustomTypeOne : CLRType
{
public override void Execute(HttpContext context)
{
//Some code that's similar across CustomTypeOne, CustomTypeTwo etc
}
public void DoStuff()
{
//Same for all CustomTypes and can be part of a base class
}
//More methods
}
public class CustomTypeTwo : CLRType
{
public override void Execute(HttpContext context)
{
//Some code that's similar across CustomTypeOne, CustomTypeTwo etc
}
public void DoStuff()
{
//Same for all CustomTypes and can be part of a base class
}
//More methods
}
Multiple Inheritance in C++ Multiple Inheritance is a feature of C++ where a class can inherit from more than one classes. The constructors of inherited classes are called in the same order in which they are inherited. For example, in the following program, B's constructor is called before A's constructor.
Multiple inheritance is a feature of some object-oriented computer programming languages in which an object or class can inherit features from more than one parent object or parent class. It is distinct from single inheritance, where an object or class may only inherit from one particular object or class.
Syntax of Implementing Multiple Inheritance in C++ Description of the syntax: access_modifier: It provides the access modifier in the class declaration as it specifies how the derived class is inheriting the base class. base_class: There can be over one base class, from which the derived class inherits its properties.
At times where I felt multiple inheritance would save the day, I realized that it really does make implementation and maintainability more challenging. MI effectively merges the public and protected namespace among the inherited classes, which can cause some ambiguity and complexity you don't need.
So, instead of an IS-A relationship, often I'm happier implementing a HAS-A relationship. The classes I would've inherited would instead be data members in the class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With