-1
public interface IA
{
  void DoSomething();
  void Calculate();

}

public interface IB
{
  void DoSomethingElse();
  void Calculate();
}

public class A : IA
{
  void DoSomething() { }
  void Calculate() {}
}

public class B : IB
{
  void DoSomethingElse() { }
  void Calculate() {}
}

public class C : IA, IB
{

    //How can I implement Calculate() in class B and DoSomething() in class A?
}

How can I avoid duplicate code in class C. Reference: How to simulate multiple inheritance in C#. I don't want to write the full methods again in class C. Thanks for any help.

4
  • 4
    But you're not "writing the full methods again". You're clearly using delegation and passing off the implementation to your embedded instances of A and B. Commented Apr 9, 2013 at 2:17
  • Yes, you're right. Sorry maybe I worded it incorrectly. In class C, instead of "public void DoSomething(){m_A.DoSomething();}" I would like to use type cast if possible Commented Apr 9, 2013 at 2:20
  • 1
    @KevenDiuliy Type cast? How would you want to use type casting? I think you must mean something else. Commented Apr 9, 2013 at 2:22
  • I think that in your sample class C you'll need to explicitly implement both IA.Calculate() and IB.Calculate(). Commented Apr 9, 2013 at 3:29

2 Answers 2

1

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();
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Hey thanks for the response, when I tried to compile this it says "the exe does not contain a static "Main" method suitable for an entry point". Would that need to be added here? thanks again
This is just definition of class C, much like you defined it in your question. The rest of the code (other classes/interfaces definitions, Main method, etc.) are just as you had it in your question
1

Note that you are not achieving multiple inheritance at all. C# has singular inheritance. You are implementing multiple interfaces.

There is absolutely no reason why you cannot implement the interfaces in classes A and B, and then have B derive from A and C derive from B. The method implementations from the interfaces can even be abstract, which forces the child class to do the implementation. If you follow this approach then you can still pass C around as IA or IB simply by casting or using the as keyword.

You can also have interface IB "inherit"1 (implement) IA, which means anything that implements IB must also implement IA.



1 When IB derives from IA it isn't inheritance like it is at the class level, it is actually still implementation. So IB implements IA, it doesn't inherit it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.