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 C class 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); }
}
Generally this is a poor design overall because you can have both A
and B
implement a method with the same name and variable types such 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 a .A
and .B
properties instead of combining the two classes.