Click here to Skip to main content
15,890,947 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to know that why multiple inheritanc not implemented in C#.
Posted

 
Share this answer
 
Comments
Member 9129971 18-Aug-15 5:22am    
Thanks, a quite nice explanation but what is :: this character , here it shows that the D would get the copy through B, but i have not used this symbol yet in programming, so what this actually is and what its use is.
see here,
B :: testing = xTesting; // use B's version of testing
Peter Leow 18-Aug-15 9:06am    
Although it rarely happens, the :: operator is used to resolve name conflicts between namespaces, classes, and members. In this case, it makes clear that it refers to the "testing" member of class B (not class C).
Member 9129971 18-Aug-15 9:56am    
Thanks
If you would have done a search you would have find a lot of answer on the web.

By the way, a similar question was already asked here on CodeProject and have more complete answers : Why C# does not support Multiple Inheritance ?[^]

Given that multiple inheritance is hard to implement and have majors impacts on the implementation of virtual tables and cast and that it was already well known that multiple inheritance cause a lot of problems, it is easy to understand why it was not supported in C#.

Although C++ support it, a lot of people would recommand to avoid using it (except from "interface"). Almost any time multiple inheritance could be used, it would be preferable to reconsider the design and use single inheritance and write wrapper fonctions that forward them to an internal object for function that would be available from an additionnal base class under multiple inheritance.

This will reduce coupling and it also allows to vary the type subobject which is not possible with multiple inheritance.


Also each potential feature has some potential benefits and also a potential of being used. In some cases, it only simplify code and in other it allows to solve completly new problems. Finally each feature can have potential problems too.

Thus choosing which feature make part of language is a decision between the PRO and CON of having it.

In the case of multiple inheritance, we have a lot of CONs:
- Hard to implement.
- Seldom used.
- High coupling.
- High maintenance cost.
- High chance of being misused.
- In most case, alternate solutions are better (for ex. composition and implementing multiple interfaces).
- Potential impact on the whole system performance (more complex code for cast and type system).
- Make the language more complex.
- Can affect all language that target to .NET.
- ...
 
Share this answer
 
v4
Virtual methods are too complex to properly implement.
 
Share this answer
 
Comments
Member 9129971 18-Aug-15 5:24am    
so you mean ,complexity is the only reason for this, does it has something to do with the .net framework?
Since it not implemented in any .net language
F-ES Sitecore 18-Aug-15 6:07am    
Yes it is the .net framework more than individual language implementations. They just decided to not implement it as handling virtual methods when a class inherits from two other classes is a tricky issue.
Philippe Mori 18-Aug-15 12:30pm    
The complexity is mainly from a maintenance perspective by abusing of it. It is far better to uses interfaces and not rely on inheritance too much for code sharing...
F-ES Sitecore 19-Aug-15 4:13am    
You have to use the right tool for the job. If that is interfaces then use them, if it is inheritance then use that. You can rarely substitute one for the other as one comes with implementation and one does not.
Philippe Mori 19-Aug-15 8:49am    
And there is the problem... You want to reuse part of the implementation even though it is not a is-a relationship... Well private or protected inheritance can then be used but somewhere down the path class hierarchy could change and then it affect all those classes.

A design that avoid multiple inheritance is almost always a better option. If one need to reuse code then it can make some helper classes instead of inheriting code. Less coupling is better for maintenance.
Given the complexity of multiple inheritance and the fact that generally avoiding it lead to better design and the fact that from C++ experience, it was known to cause as much problems as it solve, it is easy to understand why it was not supported. If it was supported, then any language targetting .NET would have to support it...

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