[c#] Does C# support multiple inheritance?

A colleague and I are having a bit of an argument over multiple inheritance. I'm saying it's not supported and he's saying it is. So, I thought that I'd ask the brainy bunch on the net.

This question is related to c# multiple-inheritance

The answer is


Multiple inheritance allows programmers to create classes that combine aspects of multiple classes and their corresponding hierarchies. For ex. the C++ allows you to inherit from more than one class

In C#, the classes are only allowed to inherit from a single parent class, which is called single inheritance. But you can use interfaces or a combination of one class and interface(s), where interface(s) should be followed by class name in the signature.

Ex:

Class FirstClass { }
Class SecondClass { }

interface X { }
interface Y { }

You can inherit like the following:

class NewClass : X, Y { } In the above code, the class "NewClass" is created from multiple interfaces.

class NewClass : FirstClass, X { } In the above code, the class "NewClass" is created from interface X and class "FirstClass".


In generally, you can’t do it.

Consider these interfaces and classes:

public class A { }    
public class B { }
public class C { }

public interface IA { }
public interface IB { }

You can inherit multiple interfaces:

class A : B, IA, IB {
  // Inherits any single base class, plus multiple interfaces.
}

But you can’t inherit multiple classes:

class A : B, C, IA, IB {
    // Inherits multiple base classes, plus multiple interfaces.
}

C# 3.5 or below does not support the multiple inheritance, but C# 4.0 could do this by using, as I remembered, Dynamics.


I recently somehow got to the same mindset, inheriting two classes into a class and ended up on this page (even though i know better) and would like to keep reference to the solution i found perfect in this scenario, without enforcing implementation of interfaces

My solution to this problem would be splitting up your data into classes that make sense:

public class PersonAddressSuper
{
    public PersonBase Person { get; set; }
    public PersonAddress Address { get; set; }

    public class PersonBase
    {
        public int ID { get; set; }
        public string Name { get; set; }
    }
    public class PersonAddress
    {
        public string StreetAddress { get; set; }
        public string City { get; set; }
    }
}

Later on in your code, you could use it like this:

Include Both Parts, Base & Address

PersonAddressSuper PersonSuper = new PersonAddressSuper();
PersonSuper.PersonAddress.StreetAddress = "PigBenis Road 16";

Base Only:

PersonAddressSuper.PersonBase PersonBase = new PersonAddressSuper.PersonBase();
PersonBase.Name = "Joe Shmoe";

Address Only:

PersonAddressSuper.PersonAddress PersonAddress = new PersonAddressSuper.PersonAddress();
PersonAddress.StreetAddress = "PigBenis Road 16";

Actually, it depends on your definition of inheritance:

  • you can inherit implementation (members, i.e. data and behavior) from a single class, but
  • you can inherit interfaces from multiple, well, interfaces.

This is not what is usually meant by the term "inheritance", but it is also not entirely unreasonable to define it this way.


Like Java (which is what C# was indirectly derived from), C# does not support multiple inhertance.

Which is to say that class data (member variables and properties) can only be inherited from a single parent base class. Class behavior (member methods), on the other hand, can be inherited from multiple parent base interfaces.

Some experts, notably Bertrand Meyer (considered by some to be one of the fathers of object-oreiented programming), think that this disqualifies C# (and Java, and all the rest) from being a "true" object-oriented language.


C# does not support multiple inheritance of classes, but you are permitted to inherit/implement any number of interfaces.

This is illegal (B, C, D & E are all classes)

class A : B, C, D, E
{
}

This is legal (IB, IC, ID & IE are all interfaces)

class A : IB, IC, ID, IE
{
}

This is legal (B is a class, IC, ID & IE are interfaces)

class A : B, IC, ID, IE
{
}

Composition over inheritance is a design pattern that seems to be favorable even in languages that support multiple inheritance.


As an additional suggestion to what has been suggested, another clever way to provide functionality similar to multiple inheritance is implement multiple interfaces BUT then to provide extension methods on these interfaces. This is called mixins. It's not a real solution but it sometimes handles the issues that would prompt you to want to perform multiple inheritance.


You cannot do multiple inheritance in C# till 3.5. I dont know how it works out on 4.0 since I have not looked at it, but @tbischel has posted a link which I need to read.

C# allows you to do "multiple-implementations" via interfaces which is quite different to "multiple-inheritance"

So, you cannot do:

class A{}

class B{}

class C : A, B{}

But, you can do:

interface IA{}
interface IB{}

class C : IA, IB{}

HTH


You can't inherit multiple classes at a time. But there is an options to do that by the help of interface. See below code

interface IA
{
    void PrintIA();
}

class  A:IA
{
    public void PrintIA()
    {
        Console.WriteLine("PrintA method in Base Class A");
    }
}

interface IB
{
    void PrintIB();
}

class B : IB
{
    public void PrintIB()
    {
        Console.WriteLine("PrintB method in Base Class B");
    }
}

public class AB: IA, IB
{
    A a = new A();
    B b = new B();

    public void PrintIA()
    {
       a.PrintIA();
    }

    public void PrintIB()
    {
        b.PrintIB();
    }
}

you can call them as below

AB ab = new AB();
ab.PrintIA();
ab.PrintIB();

Sorry, you cannot inherit from multiple classes. You may use interfaces or a combination of one class and interface(s), where interface(s) should follow the class name in the signature.

interface A { }
interface B { }
class Base { }
class AnotherClass { }

Possible ways to inherit:

class SomeClass : A, B { } // from multiple Interface(s)
class SomeClass : Base, B { } // from one Class and Interface(s)

This is not legal:

class SomeClass : Base, AnotherClass { }

Multiple inheritance is not supported in C#.

But if you want to "inherit" behavior from two sources why not use the combination of:

  • Composition
  • Dependency Injection

There is a basic but important OOP principle that says: "Favor composition over inheritance".

You can create a class like this:

public class MySuperClass
{
    private IDependencyClass1 mDependency1;
    private IDependencyClass2 mDependency2;

    public MySuperClass(IDependencyClass1 dep1, IDependencyClass2 dep2)
    {
        mDependency1 = dep1;
        mDependency2 = dep2;
    }

    private void MySuperMethodThatDoesSomethingComplex()
    {
        string s = mDependency1.GetMessage();
        mDependency2.PrintMessage(s);
    }
}

As you can see the dependecies (actual implementations of the interfaces) are injected via the constructor. You class does not know how each class is implemented but it knows how to use them. Hence, a loose coupling between the classes involved here but the same power of usage.

Today's trends show that inheritance is kind of "out of fashion".


Simulated Multiple Inheritance Pattern
http://www.codeproject.com/KB/architecture/smip.aspx enter image description here


C# does not support multiple inheritance built in.

For adding multiple inheritance to languages that does not support it, You can use twin design pattern


You may want to take your argument a step further and talk about design patterns - and you can find out why he'd want to bother trying to inherit from multiple classes in c# if he even could


It does not allow it, use interface to achieve it.

Why it is so?

Here is the answer: It's allowed the compiler to make a very reasoned and rational decision that was always going to consistent about what was inherited and where it was inherited from so that when you did casting and you always know exactly which implementation you were dealing with.