Multiple inheritance in C# (how to fake it)

Diamond Inheritance

C# doesn’t allow for multiple inheritance, as Microsoft made a design decision when creating the .NET framework not to do so. Presumably, this is to avoid situations such as the diamond problem from occurring in the languages, among others.

However, what if we want to share methods or properties across classes that already have differing base classes?

The Problem

Say we have the following four classes:

Before extension methods came about, if we want to write a method that allows us to do something common with a class that contains a CoolProperty, we could do some maneuvering with interfaces like so:

But then calling these methods was somewhat of a chore, as it wasn’t obvious that they were available to us. We could write a utility class that has a method that takes in an IHasACoolProperty:

And we call this method like so:

The Solution

Now, with extension methods, we can fake multiple inheritance by continuing to apply an interface to each of our classes that need the shared methods, if we define our method like this (notice the this keyword in the method declaration. This turns it into an extension method):

Then, if we import the namespace containing the extension class, we can call this method like so:

This allows us to make A and B appear to have a common base class, even though they do not.

If this post helped you, please share it. If it doesn’t make sense or you have questions, then someone else might have the same question; leave a comment. Thanks for taking the time to read this!

UPDATE:

I received a request via G+ for an example of when multiple inheritance would be needed. In reality, given proper design, there is no need to use multiple inheritance. The .NET framework (C#, specifically) has been around since 2002 without support for it and has remained extremely popular, so coders must be getting along fine without it.

This situation really goes back to the “is a” vs. “has a” (or inheritance vs. composition) question in object-oriented programming. You’ll notice in the example above that the interface I defined was called IHasACoolProperty, implying that classes that implement the interface have a given property.

Let’s look at a different example to illustrate this further. Say we are using a model of a set of vehicles:

And we extend Car with some classes of our own:

And Truck as well:

Now, let’s also assume, for the sake of this example, that the Vehicle, Car, and Truck classes are in an assembly over which we have no control (the assembly is precompiled and we don’t have the source.) But we get to the point where we want to add a property that is common to Sedan, Coupe, and FordF150. Here we can use “fake” multiple inheritance.

We can change neither the Car nor Truck classes because they are compiled already. But we can add an interface to add the property we want:

Now, we can add a utility class that does something with NumberOfWindows:

And again, if we import the namespace containing the VehicleUtlity class, we can call this method as if it is on Sedan, Coupe, or FordF150:

I hope this clarifies things further. If you have any more questions, please don’t hesitate to ask. Thanks again for reading!

Leave a Reply

Your email address will not be published. Required fields are marked *


× seven = 56

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">