What is the best way to achieve multiple inheritance in .NET ?
Printable View
What is the best way to achieve multiple inheritance in .NET ?
Have one class to inherit from and several Interfaces.
Another option that is sometimes worth exploring is hierarchical inheritance.
Although each class is only inheriting from one parent that parent can inherit from another.Code:ClassA
{
}
ClassB : ClassA
{
}
ClassC : ClassB
{
}//etc
The benefit of this method is that the methods and properties that are inherited can be implemented in the preceding generations, unlike interfaces where no implementation is possible, so you have to implement every inherited method.
C++ is the only language in Visual Studio that supports genuine multiple inheritence. Even then, I'm not sure but I have a feeling that you may only be able to use it in unmanaged code anyway. Implementing multiple Interfaces, as mendhak suggested, is as close as you can get to multiple inheritence in other Visual Studio languages, but while implementing an interface and inheriting a class are similar they are certainly not the same.
Local help: ms-help://MS.MSDNQTR.2003FEB.1033/vbls7/html/vblrfVBSpec4_2_2.htm
MSDN online: http://msdn.microsoft.com/library/de...BSpec4_2_2.asp
True multiple inheritance is either superb or a nightmare.
If you are programming in C++ and you are the only person writing the project and you never forget things, it is a real bonus.
However, if there is anyone else writing the classes you want to also inherit from it can become a minefield.
If I write a class with a method foo() and then inherit from it that's ok, if I then inherit from a class you've written and your class also has a method foo() we have problems.
Some compilers have a hissy fit, worse some don't, so you don't know which foo() is called, if either, or if you will get a run-time error.
So I think I prefer hierarchical inheritance and interfaces, and the little bit extra coding that entails.
Absolutely. It's not just a twist of fate the Microsoft hasn't included multiple inheritence in the other Visual Studio languages. They have specifically omitted it to avoid those very issues. Multiple inheritence may be handy at times but it's too easy to mess up, so it creates more problems than it solves.Quote:
Originally Posted by GlenW