Click here to Skip to main content
15,312,353 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
This may be a silly question but want answer for this.

I come to know that C# doesn't allow direct multiple inheritance. So Interfaces are used.

But my question is that interface only has signatures. But abstract has concrete method and other Base classed can have their own methods.

So is there any way to achieve perfect multiple inheritance.


Note: Kindly understand this scenario before down voting.

What I have tried:

It works in C++ but not in C#. Will share that code if required.
Posted
Updated 28-Jun-21 19:31pm

Because the language designers decided not to allow it.

There is a deeper reason for this in that it adds a lot of complexity with not a lot of "real" benefit, and the reason for that is called the Diamond Problem: Multiple inheritance - Wikipedia[^] covers it well.
While there are ways round this - C++ allows it - they complicate things a lot and Interfaces provide most of the required functionality, especially now they can contain default methods from C#8.0
   
The answer to your question : So is there any way to achieve perfect multiple inheritance. ?  is NO.
The C# language is designed in that way. This may be to avoid the diamond problem which we see in C++. the workaround to this is using interfaces in C#. Two classes can implement the two interfaces , re-define the required methods. Then third class can implement the interfaces and then create objects of those two classes to invoke the methods.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900