Why multiple inheritance of classes is not supported in C#?


Multiple inheritance of classes is not supported in C# because of the diamond problem.
Diamond Problem: If in a scenario there is a Class A have a Print method. And another classes B and C is overriding Class A Print method with single inheritance concept. Now we have a class D which inherit both these classes B and C with multiple inheritance concept.

Diamond Problem

Diamond Problem

Example :

class A
{
    public virtual void Print()
    {
        Console.Write("Print A");
    }
}

class B : A
{
    public override void Print()
    {
        Console.Write("Print B");
    }
}

class C : A
{
    public override void Print()
    {
        Console.Write("Print C");
    }
}

class D : B, C
{        
}


Now when we will create the object of Class D and call the Print method. Compiler will be confused which parent class method to call. Hence multiple inheritance of classes is not supported in C#.

One thought on “Why multiple inheritance of classes is not supported in C#?

Comments are closed.