Why C# doesn't support multiple inheritance?

 

Inheritance is used to combine the different classes functionality. Single inheritance means one class is inheriting another class which is supported by C#. Multiple inheritance means one class inheriting multiple classes which is not supported by C#. We discuss why C# doesn't support multiple inheritance.

 

C++ supports multiple inheritance, but Microsoft avoids the multiple inheritance in .Net. Multiple inheritance creating complexity while designing any solution. For example we have three classes ClassA, ClassB and ClassC. Here ClassC inherits ClassA & ClassB as shown below.

 

class ClassA

{

        void GetData()

        {

       

        }

}

 

class ClassB

{

        void GetData()

        {

 

        }

}

 

class ClassC : ClassA, ClassB

{

       

}

 

Here ClassC is inheriting ClassA and ClassB which has method with same name GetData(). If you want to class GetData() by creating object for ClassC, the object confuses which method it has to call whether ClassA method or ClassB method. To avoid this type of confusion C# avoids the multiple inheritance for classes.

 

For example if ClassA and ClassB has different methods you can get multiple inheritance as shown below.

 

class Program

{

        static void Main(string[] args)

        {

            ClassC obj = new ClassC();

            obj.GetData1();

            obj.GetData2();

        }

}

 

class ClassA

{

        public void GetData1()

        {

       

        }

}

 

class ClassB : ClassA

{

        public void GetData2()

        {

 

        }

}

 

class ClassC : ClassB

{

       

}

 

As shown above ClassB inherits ClassA and ClassC inherits ClassB. In this way we are getting multiple inheritance.

 

You can implement multiple interfaces which has same method as explained in my previous article Implementing Multiple Interfaces which have same method name and same signature in C#.