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.
Multiple Inheritance and Objects C#
C# does not support multiple inheritance of classes at a single level. C# does support multiple levels of inheritance in a hierarchy. What you are seeing is that at the top of your inheritance hierachy is System.Object
, which is the top level base class for classes and structs in .NET.
What you cannot have is:
class A { }
class B { }
class C : A, B { }
What you can have:
class A { }
class B : A { }
class C : B { }
Make sense?
For a bit more completeness, C# does allow you to simulate multiple inheritance via the usage of interfaces. So let's walk it back.
class A { }
interface B { }
class C : A, B { } // legal
In this example, the declaration of C is legal, and you would need to implement the various members that interface B defines as part of C.
How to simulate multiple inheritance in C#
C# does not have multiple inheritance, so the line
Class C : A , B {}
will never work. You can do similar things with interfaces though, along the lines of
interface InterfaceA { void doA(); }
class A : InterfaceA { public void doA() {} }
interface InterfaceB { void doB(); }
class B : InterfaceB { public void doB() {}}
class C : InterfaceA, InterfaceB
{
m_A = new A();
m_B = new B();
public void doA() { m_A.doA(); }
public void doB() { m_B.doB(); }
}
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
C# Multiple Inheritance with Interfaces
Assuming that IA.Calculate()
is not the same as IB.Calculate()
and you therefore can't just make IB
inherit from IA
, you can implement both interfaces in C
by delegating execution on private instances of A
and B
:
public class C : IA, IB
{
private A _a;
private B _b;
public C()
{
this._a = new A();
this._b = new B();
}
public void DoSomething()
{
this._a.DoSomething();
}
void IA.Calculate()
{
this._a.Calculate();
}
public void DoSomethingElse()
{
this._b.DoSomethingElse();
}
void IB.Calculate()
{
this._b.Calculate();
}
}
multiple inheritance in c#.net - abstract classes
No you can only inherit from one class whether it is abstract or concrete.
Here is link to a question that has quite a detailed discussion of abstract vs base classes
Interface vs Base class
Related Topics
The Entity Cannot Be Constructed in a Linq to Entities Query
Casting VS Using the 'As' Keyword in the Clr
Random Number Generator Only Generating One Random Number
Linq'S Distinct() on a Particular Property
What Is the Correct Way to Create a Single-Instance Wpf Application
What Are Good Ways to Prevent SQL Injection
Fastest Way of Inserting in Entity Framework
Sending Email in .Net Through Gmail
Is There a Constraint That Restricts My Generic Method to Numeric Types
How to Check If a File Is in Use
Cs0120: an Object Reference Is Required For the Nonstatic Field, Method, or Property 'Foo'
Windows Service Not Shown in Add Remove Programs Under Control Panel
Send Http Post Request in .Net
How to Convert Json to Xml or Xml to Json
Extension Method and Dynamic Object
Validation Failed For One or More Entities. See 'Entityvalidationerrors' Property For More Details