Click or drag to resize
Multiple Inheritance

.NET does not support multiple inheritance (MI). It supports interface multiple inheritance only, which cannot replace real multiple inheritance class hierarchies.

There are basically two ways of simulating MI: one is, in short, to create a parallel interface hierarchy that mirrors exactly the class structure of the application and to wrap all methods into static methods; the second technique is to use delegate instances of the second base classes and generate instance wrappers.

The first method is good for compilers and generation of IL code because the kind of source code that needs to be generated is very convoluted and hard to understand and maintain. The second method is the most readable and it is the same approach that a developer would take when writing code and there is a shared class that is not part of the single-inheritance class hierarchy.

To explain simply: all first base-classes are generated normally since .NET fully support single-inheritance, all second-base classes are generated as an internal instance of the class, then all fields and methods are generated in the derived class as wrappers that redirect the call to the second-base instance. The end result is that the derived class contains all the "inherited" fields and functions but the implementation is in one single place: the second-base class.

For example, the class hierarchy shown below:

Simple multiple inheritance
embim 24

Is generated as follows:

C#
public class CBase1 : SalFunctionalClass
{
}
 
public class CBase2 : SalFunctionalClass
{
 public CBase2(object derived)
 {
         this._derived = derived;
 }
}
 
public class CDerived : CBase1
{
 private CBase2 _CBase2 = null;
 public CDerived()
 {
         this._CBase2 = new CBase2(this);
 }
}

As you can see, the derived class contains an instance of the second-base class CBase2, and the instance of CBase2 also references the derived class by saving the reference to the special internal member _derived.