Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Inheritance in C#: What is the purist way of achieving what I am trying to do?

Tags:

c#

.net

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

}
like image 246
Nick Avatar asked Jul 07 '10 14:07

Nick


People also ask

What is multiple inheritance example?

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.

Which is the multiple inheritance?

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.

What is the syntax of multiple inheritance?

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.


1 Answers

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.

like image 72
spoulson Avatar answered Oct 24 '22 03:10

spoulson