A colleague and I are having a bit of an argument over multiple inheritance. I'm saying it's not supported and he's saying it is. So, I thought that I'd ask the brainy bunch on the net.
Ask him to provide some code showing multiple inheritance in C#...
There's a good MSDN blog post discussing why C# doesn't support it.
C# 3.5 or below does not support the multiple inheritance, but C# 4.0 could do this by using, as I remembered, Dynamics.
Sorry, you cannot inherit from multiple classes
. You may use interfaces or a combination of one class and interface(s)
, where interface(s) should be followed by class name in the signature.
interface A
{ }
interface B
{ }
Class Base {}
Class AnothrClass {}
Possible ways to inherrit:
class SomeClass : A, B { } // from multiple Interface(s)
class SomeClass : Base, B { } // from one Class and Interfacce(s)
This is not legal:
class SomeClass : Base, AnothrClass { }
Like Java (which is what C# was indirectly derived from), C# does not support multiple inhertance.
Which is to say that class data (member variables and properties) can only be inherited from a single parent base class. Class behavior (member methods), on the other hand, can be inherited from multiple parent base interfaces.
Some experts, notably Bertrand Meyer (one of the fathers of object-oreiented programming), think that this disqualifies C# (and Java, and all the rest) from being a "true" object-oriented language.
Actually, it depends on your definition of inheritance:
- you can inherit implementation (members, i.e. data and behavior) from a single class, but
- you can inherit interfaces from multiple, well, interfaces.
This is not what is usually meant by the term "inheritance", but it is also not entirely unreasonable to define it this way.
As an additional suggestion to what has been suggested, another clever way to provide functionality similar to multiple inheritance is implement multiple interfaces BUT then to provide extension methods on these interfaces. This is called mixins. It's not a real solution but it sometimes handles the issues that would prompt you to want to perform multiple inheritance.
You may want to take your argument a step further and talk about design patterns - and you can find out why he'd want to bother trying to inherit from multiple classes in c# if he even could
You cannot do multiple inheritance in C# till 3.5. I dont know how it works out on 4.0 since I have not looked at it, but @tbischel has posted a link which I need to read.
C# allows you to do "multiple-implementations" via interfaces which is quite different to "multiple-inheritance"
So, you cannot do:
class A{}
class B{}
class C : A, B{}
But, you can do:
interface IA{}
interface IB{}
class C : IA, IB{}
HTH