views:

897

answers:

12

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.

+17  A: 

Nope, use interfaces instead! ^.^

Neurofluxation
Surely as Senior .Net Developer you could tell him the answer :=P
Chris S
Note that you can write extension methods for interfaces, which can help to share implementation code.
TrueWill
-1 because:You cannot do "multiple inheritance" from interfaces, you can "implement" multiple interfaces - which IMHO is a completely different concept.The short answer to the question is till 3.5, you cannot do multiple inheritance in C#
Sunny
supporting interfaces is not the same thing as multiple inheritance.
darren
@Sunny: what changed in 3.5, in your opinion?
Konrad Rudolph
Sunny
+11  A: 

Ask him to provide some code showing multiple inheritance in C#...

Paolo Tedesco
+7  A: 

There's a good MSDN blog post discussing why C# doesn't support it.

statenjason
+6  A: 

C# 3.5 or below does not support the multiple inheritance, but C# 4.0 could do this by using, as I remembered, Dynamics.

Gnought
That is a very good point. Off to read about Dynamics.
Ardman
interesting feature... here is an article about just thathttp://www.codingday.com/multiple-inheritance-in-c-using-dynamic-features/
tbischel
+1 for a clever idea, although I don't recommend using this.
Gabe Moothart
@tbischel Be sure to make note of the last comment on that article. The provided sample does not appear to work.
statenjason
While this **might** work, it's actually really ugly and requires a lot of reflection at runtime to get the job done. I would never allow such a solution in production code.
Scott Dorman
+6  A: 

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  { }
Asad Butt
+1 for differentiating between implementation inheritance and interface inheritance
Gabe Moothart
+1 for nice clear code example. A picture speaks 1000 words, not sure how many a code example speaks... but it's good anyway.
Ian
+1  A: 

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.

Loadmaster
I think Bertrand Meyer is a genius, but I would hardly call him one of the fathers of object-oriented programming. When he designed Eiffel in the 1980s, object-oriented programming was already 20 years old. It is true that some of the fathers of object-oriented programming do not consider C#, Java, C++ and co. to be object-oriented, but that doesn't have anything to do with the lack of multiple inheritance. (In fact, they don't consider inheritance to be essential at all, e.g. both Simula and Smalltalk-71 did not support inheritance.) It's the lack of message sending that bothers them.
Jörg W Mittag
Meyer defined seven criteria for "object-oriented", one of which is multiple inheritance. I'm not saying I agree with him, but he is not alone in this opinion. C++, it should be noted, has multiple inheritance.
Loadmaster
I also defined seven criteria for "object-oriented", one of which is being purple. However, since I didn't invent object-orientation, my criteria are utterly irrelevant and so are Meyer's. Alan Kay invented object-orientation (or more precisely, he invented the *word* "object-orientation"), so he and *only* he gets to decide what it means, and he has been very clear about it: "OOP to me means only messaging, local retention and protection and hiding of state-process, and extreme late-binding of all things" and "I made up the term object-oriented, and I can tell you I did not have C++ in mind."
Jörg W Mittag
C# doesn't support messaging. It does somewhat support information hiding, but it doesn't enforce it. And even with the `dynamic` features in C# 4.0, it only supports "*somewhat* late-binding of *some* things" and certainly not "*extreme* late-binding of *all* things." Please note that Alan Kay would be the first to readily admit that his *own* language, Smalltalk, doesn't exactly follow this ideal, either. Which is why he wanted to stop working on Smalltalk in 1975 or so, and develop a better language (and indeed, Smalltalk-76 and Smalltalk-80 were no longer designed by him).
Jörg W Mittag
In the system he is currently working on, for example, the language *syntax* is late-bound (like in Smalltalk-71, actually). The language *semantics* are late-bound. The type system is late-bound. Even the decision of whether or not to have a type system *at all* is late-bound. Heck, even the definition of what "late-bound" means is late-bound.
Jörg W Mittag
@Jörg: Saying that only one person gets to decide what is and what is not OO is absurd. The computer science scholar community at large ultimately decides what is OO, after much peer review and practical experience. That's the way it worked for structured programming, operating system theory, apect programming, patterns, etc. Meyer's opinions are held in higher regard than you indicate, BTW; see http://en.wikipedia.org/wiki/Object-Oriented_Software_Construction.
Loadmaster
+1  A: 

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.

Jörg W Mittag
A: 

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.

Jaxidian
Strictly speaking, .NET does not provide support for true mixins. Instead, it provides extension methods which serve some of the same uses and give some of the same benefits as mixins.
Scott Dorman
Agreed. This is a partial mixin implementation in the technical sense. However, I've heard quite a few people still refer to this as ".NET Mixins". But you are correct.
Jaxidian
Mixins are kind of a dual to interfaces: with classes, you inherit both implementation and interface, with interfaces you inherit only interface and no implementation, with mixins you inherit only implementation and no interface. Note that mixins serve a different purpose, though: mixins are used to *compose* classes (or objects in a classless language), so they are a *smaller* unit of the system than a class/interface/object. One class/object is made up of several mixins.
Jörg W Mittag
A: 

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

Jason M
+1  A: 

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

Sunny
+2  A: 

C# FAQ

drorhan