Does C# Support Multiple Inheritance

Multiple Inheritance in C#

Since multiple inheritance is bad (it makes the source more complicated) C# does not provide such a pattern directly. But sometimes it would be helpful to have this ability.

C# and the .net CLR have not implemented MI because they have not concluded how it would inter-operate between C#, VB.net and the other languages yet, not because "it would make source more complex"

MI is a useful concept, the un-answered questions are ones like:- "What do you do when you have multiple common base classes in the different superclasses?

Perl is the only language I've ever worked with where MI works and works well. .Net may well introduce it one day but not yet, the CLR does already support MI but as I've said, there are no language constructs for it beyond that yet.

Until then you are stuck with Proxy objects and multiple Interfaces instead :(

Does C# support multiple inheritance?

Nope, use interfaces instead! ^.^

C# Multiple Inheritance

One possible solution would be to modify your hierarchy:

public class PersonWithApprove : Person { // TODO: replace with non disgusting name
public bool Approved { get; set; }
// etc...
}

public class Student : PersonWithApprove {
}

public class Faculty : PersonWithApprove {
}

Or you could create an interface:

public interface IApprove {
bool Approved { get; set; }
// etc
}

public class Student : Person, IApprove {
}

You might also leave the class Approve as such, and have classes with a property of that type:

public class Student : Person {
Approve _approve = new Approve();
public Approve Approve {
get { return _approve; }
}
}

inherit from two classes in C#

Multitiple inheritance is not possible in C#, however it can be simulated using interfaces, see Simulated Multiple Inheritance Pattern for C#.

The basic idea is to define an interface for the members on class B that you wish to access (call it IB), and then have C inherit from A and implement IB by internally storing an instance of B, for example:

class C : A, IB
{
private B _b = new B();

// IB members
public void SomeMethod()
{
_b.SomeMethod();
}
}

There are also a couple of other alternaitve patterns explained on that page.

Does C# support multiple inheritance 4.0?

They probably meant can a class derive from two or more base classes, which it cannot. Confusing wording.

public abstract class A1;
public abstract class A2;
public abstract class B : A2;
public class C : A1, A2; // invalid
public class D : B; // valid

Need multiple inheritance functionality in C#. What am I doing wrong?

You can only inherent from a single base class in C#. However, you can implement as many Interfaces as you'd like. Combine this fact with the advent of Extension Methods and you have a (hacky) work-around.

Confused about multiple inheritance

Each class inherits from one other class. If you make your own class extend one of your other classes, then it will not directly inherit from Object but rather from your superclass. The superclass in its turn will inherit from Object.

Your subclass will have all the methods from Object available trough the superclass.

This will illustrate it:

void Main()
{
Console.WriteLine (new Super().GetType().BaseType);
Console.WriteLine (new Sub().GetType().BaseType);
}

class Super { }
class Sub : Super { }

Output:

Sample Image



Related Topics



Leave a reply



Submit