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; }
}
}
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.
Why C# doen't support multiple inheritance
Using interfaces is more flexible and eliminates the ambiguity of multiple inheritance.
Further details, HERE.
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.
Related Topics
Does Anyone Still Use [Goto] in C# and If So Why
Will Code in a Finally Statement Fire If I Return a Value in a Try Block
Synchronously Waiting for an Async Operation, and Why Does Wait() Freeze the Program Here
How to Wait for a Backgroundworker to Cancel
An ASP.NET Setting Has Been Detected That Does Not Apply in Integrated Managed Pipeline Mode
Entity Framework - Code First - Can't Store List<String>
Multiple Types Were Found That Match the Controller Named 'Home'
Addbusinessdays and Getbusinessdays
How to Install a Windows Service Programmatically in C#
How to Create a Wpf Usercontrol with Named Content
Readonlycollection or Ienumerable for Exposing Member Collections
Generate and Sign Certificate Request Using Pure .Net Framework
Merge Two (Or More) Lists into One, in C# .Net
Convert a Binary String Representation to a Byte Array