Inherit from two classes in C #

Possible duplicate:
Multiple Inheritance in C #

I have two classes Class A and Class B. These two classes cannot inherit from each other. I am creating a new class called Class C. Now I want to implement the methods of class A and class B by inheritance. I know that multiple inheritance is not possible in C #, but is there any other way to do this?

thanks

+7
source share
7 answers

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

The basic idea is to define an interface for the members of class B that you want to access (call it IB ), and then C inherit from A and implement IB internally while saving 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 alternateve templates on this page.

+27
source

A common alternative to inheritance is delegation (also called composition): X "has" Y, not X ", this is" Y. Therefore, if A has functionality for working with Foos, and B has functionality for working with Bars, and you want both in C, then something like this:

 public class A() { private FooManager fooManager = new FooManager(); // (or inject, if you have IoC) public void handleFoo(Foo foo) { fooManager.handleFoo(foo); } } public class B() { private BarManager barManager = new BarManager(); // (or inject, if you have IoC) public void handleBar(Bar bar) { barManager.handleBar(bar); } } public class C() { private FooManager fooManager = new FooManager(); // (or inject, if you have IoC) private BarManager barManager = new BarManager(); // (or inject, if you have IoC) ... etc } 
+7
source

If you want to literally use the method code from A and B , you can make class C contain an instance of each of them. If you code interfaces for A and B , then your clients should not know that you give them C , not A or B

 interface IA { void SomeMethodOnA(); } interface IB { void SomeMethodOnB(); } class A : IA { void SomeMethodOnA() { /* do something */ } } class B : IB { void SomeMethodOnB() { /* do something */ } } class C : IA, IB { private IA a = new A(); private IB b = new B(); void SomeMethodOnA() { a.SomeMethodOnA(); } void SomeMethodOnB() { b.SomeMethodOnB(); } } 
+6
source

You want to say that class C will be the base class for A and B in this case.

 public abstract class C { public abstract void Method1(); public abstract void Method2(); } public class A : C { public override void Method1() { throw new NotImplementedException(); } public override void Method2() { throw new NotImplementedException(); } } public class B : C { public override void Method1() { throw new NotImplementedException(); } public override void Method2() { throw new NotImplementedException(); } } 
+4
source

You can define a base class for A and B , where you can use common methods / properties / fields.

After implementing C:Base .

Or, to simulate multiple inheritance, define a common interface (s) and implement them in C

Hope this helps.

+3
source

Make two interfaces IA and IB :

 public interface IA { public void methodA(int value); } public interface IB { public void methodB(int value); } 

Next, make A implement IA and B implement IB .

 public class A : IA { public int fooA { get; set; } public void methodA(int value) { fooA = value; } } public class B : IB { public int fooB { get; set; } public void methodB(int value) { fooB = value; } } 

Then implement your class C as follows:

 public class C : IA, IB { private A _a; private B _b; public C(A _a, B _b) { this._a = _a; this._b = _b; } public void methodA(int value) { _a.methodA(value); } public void methodB(int value) { _b.methodB(value); } } 

This is usually a bad design because you can use the A and B method with the same name and variable types as foo(int bar) , and you will need to decide how to implement it, or if you just call foo(bar) on both _a and _b . As suggested elsewhere, you should consider the .A and .B properties instead of combining the two classes.

+3
source

Use composition :

 class ClassC { public ClassA A { get; set; } public ClassB B { get; set; } public C (ClassA a, ClassB b) { this.A = a; this.B = b; } } 

Then you can call CADoA() . You can also change the properties to an interface or an abstract class, such as public InterfaceA A or public AbstractClassA A

+2
source

All Articles