Click here to Skip to main content
15,311,151 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Does C# support multiple inheritance? Either way give some example.

Only a weak form of multiple inheritance: strictly one parent class, but multiple interfaces are allowed. The only exclusion is the class System.Object which does not have a base class. If no base class is specified, it means that a base class still exist; this implied base class is System.Object.

interface MyInterface1 { void Method(); }
interface MyInterface2 { void Method(); }

//this is fine:
class MyClass : System.Windows.Form, MyInterface1, MyInterface2 {
    void MyInterface1.Method() {}
    void MyInterface2.Method() {}

//this will not compile, failed at attempt to add another base class MyClass:
class MyClass : System.Windows.Form, MyClass, MyInterface1 {
    void MyInterface1.Method() {}

Dima Popov 3-Mar-11 16:19pm
Surely a better answer.
Thank you.
AHBB 3-Mar-11 16:20pm
class P:c1 //how to call c2 & c3 after c1?

class c1
{ }
class c2
{ }
class c3
{ }
Incorrect question. Classes are not called, method and properties are (yes, properties, too).
If you question is about the order of declaration -- it does not matter. This is not C/C++!

AHBB 3-Mar-11 16:44pm
You're welcome.
Thank you for accepting my Answer.

Good luck, call again.
We can use multiple inheritance in c++ but C# never support for multiple inheritance. Instead of that we can use interface concept in C#.
No, but internally every class inherits object.
AHBB 3-Mar-11 16:16pm
no clear
Incomplete answer, also, you "but" is irrelevant.
Dima Popov 3-Mar-11 16:23pm
Is that just irrelevant or technically incorrect?
Dima, "No" is questionable, because of the term "weak form of multiple inheritance". You see, technically interface layout matches the layout of the class with no data and all abstract method (as it was historically implemented this way and makes full sense); I mean "weak form of multiple inheritance" is still multiple inheritance. Enough said. As to the System.Object as a common ancestor -- it is the unrelated fact. One can create systems with singular or multiple inheritance and with single or multiple common ancestors in all combination without interference between these two factors -- these aspects are orthogonal to each other.

I'm sorry. Mentioning of System.Object still makes the following sense: in the sense of "multiple inheritance" what multiplicity is applied to a .NET class? 0..INF for base interfaces, strictly 1 (not 0) for base classes. Why not 0? Because if no base class is declared, there is an implied base class which is System.Object.
"every class inherits object" in not quite accurate, because you need to make difference between direct and indirect inheritance. In the indirect sense, this can be inferred from the above rule.
Dima Popov 4-Mar-11 1:48am
Thank you for taking your time to answer. I learned a lot.
You're very welcome.
AHBB 3-Mar-11 16:41pm
Dima Popov 3-Mar-11 16:56pm
SA gave you the full answer. What I meant was that you can't declare a class like C: B, A though C: B is actually C: B, object. Please, correct me if I'm wrong
This[^] might give you an idea.

Did you try it? 5 minutes of coding would have answered the question. I'm sure your teacher wants you to find out yourself.

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