Possible Duplicate:
Does C# support multiple inheritance?
Does C# support multiple inheritance?
C# does not support multiple inheritance. It does support multiple implementation of interfaces (so your class can uses as many interfaces as it likes, only one base class tho')
In short No, multiple inheritance is not supported.
This is not a problem as inheritance shall always translate to a "is-a" relationship. Objects usually are only of one kind.
On the other side interfaces, that translate to a "behaves like" relationship can be implemented multiple times.
This is not a limitation but rather an advantage to avoid many strange issues like the Diamond Problem.
No. Sorry. It currently does not support multiple inheritance.
It does not support multiple inheritance, and hopefully it never does. Getting rid of this error source was one of the major steps away from the C++ programming model.
Thomas
Multiple Inheritance is always a creepy concept when you conceptualize with real world examples. Multiple Inheritance means Multiple Fathers. Objects are safe in C# they belong to only one father :)
On a different threat they have discussed some of the design problems it has. This must interest you: What is the exact problem with multiple inheritance?
Multiple inheritance is not supported in C#.
But if you want to "inherit" behavior from two sources why not use the combo:
Composition
and
Dependency Injection
There is a basic but important OOP principle that say: "Favor composition over inheritance".
You can create a class like this:
public class MySuperClass
{
private IDependencyClass1 mDependency1;
private IDependencyClass2 mDependency2;
public MySuperClass(IDependencyClass1 dep1, IDependencyClass2 dep2)
{
mDependency1 = dep1;
mDependency2 = dep2;
}
private void MySuperMethodThatDoesSomethingComplex()
{
string s = mDependency1.GetMessage();
mDependency2.PrintMessage(s);
}
}
As you can see the dependecies (actual implementations of the interfaces) are injected via the constructor. You class does not know how each class is implemented but it knows how to use them. Hence a loose coupling between the classes involved here but the same power of usage.
Today's trends show that inheritance is kind of "out of fashion".
My 2 cents: as has already been said, C# doesn't support multiple inheritance as also other OO languages like Java. The main reason is the problems that multiple inheritance brings, like the use of the super() constructors, as example; it becomes difficult to evaluate all the possible ambiguities -- from which class does the super() comes from? Also there is the problem of performance because of the ways to solve the ambiguities. Most of this problems where discovered with C++ that supports multiple inheritance and, with that in mind, the developers
As Svisstack said, use interfaces. Most of the time is the best way to solve your problem. By experience, even in C++ I use abstract classes without any attribute to simulate an interface.
PS: as I was writting this answer, jdehaan present what I was saying more detailed.
While it doesn't support multiple inheritance you can push multiple implementations via interfaces and abstracts. An example would be creating a vehicle and implementing different interfaces based on the type of vehicle. Below is a very pseudo example of a plane and a taxi which both implement the vehicle class but then also implement the interface specific to the type of vehicle and contains method specific to their actions.
The way I typed the method calls is known as explicit interface implementation and handles an event where say both the vehicle and other interfaces contain the same method.
interface IPlane
{
void CheckLanding();
void LandingGear();
void Turn();
}
interface IVehicle
{
void Gas();
void Brake();
void Turn();
}
interface ITaxi
{
void StartMeter();
void StopMeter();
)
public class Taxi: IVehicle, ITaxi
{
void ITaxi.StartMeter()
void IVehicle.Gas
void IVehicle.Brake
void ITaxi.StopMeter()
{
public class AirPlane: IVehicle, IPlane
{
void IVehicle.Gas()
void IVehicle.Turn()
void IPlane.LandingGear()
void IPlane.Turn()
void IPlane.CheckLanding()
void IPlane.Turn()
void IPlane.LandingGear()
void IVehicle.Brake()
void IVehicle.Turn()
}