In every .net interview there is a common question is "why multiple inheritances is not allowed in C#?"
So, the answer is when we try to implement multiple inheritances using multiple base classes diamond shape problem occurs.
Then definitely next question will be what is the diamond shape problem?
Suppose we have Class A and Class A inherited in Class B and Class C, and finally when we want to inherit Class B and Class C in Class D. In this case diamond shape problem occurs. The means method from Class is overridden in Class B and Class C, when we try to inherit Class B and Class C in Class D in this case compiler will get confused about which methods need to use from Class B or Class C.
So, to overcome above said diamond shape problem we can use interfaces. And by using interfaces we can achieve multiple inheritances.
Multiple inheritance using Interfaces
We will create two interfaces with the same method declaration as below.
We will implement the above-declared interfaces to the class as below.
And finally, we call methods from different interfaces which have the same definition and implementation.
Complete source code of above implementation:
The above code is written in Visual Studio 2019 and .net framework 4.6.1 and C# version 7.3.
Since C# 8 version a new feature introduced for the interface is we can write default implementation inside the interface.
Inside the below code written in C# 8(.net 5) implemented this new feature called default method implementation in the interface.