1

Possible Duplicate:
Multiple Inheritance in C#

I know that C# does not support multiple inheritance in classes.

However, I need to do add the feature of both ClassA and ClassB in MyClass:

public class ClassA
{
    /* Third party class*/
}
public class ClassB
{
    /* Third party class*/
}

public class MyClass : ClassA, ClassB
{
    /*Compilation error*/
}

Is there any way I can achieve this?

Community
  • 1
  • 1
D J
  • 6,908
  • 13
  • 43
  • 75

5 Answers5

8

You can only have multiple interface inheritance in C#. The best you can do with these third party classes is to encapsulate an instance of each in MyClass and re-create each function call and pass it to the appropriate instance.

public class MyClass
{
    ClassA a = new ClassA();
    ClassB b = new ClassB();

    void AMethod()
    {
       a.AMethod();
    }

    int BMethod(int value)
    {
      return b.BMethod(value);
    }
}

If you need to be able to use this class in locations where a ClassA, say, is required, then you can add conversion operators to convert it back to ClassA or ClassB. Whether it's more appropriate at that point to return new instances of these or expose your internal ones is one you would need to carefully consider.

Obviously, if ClassA or ClassB do implement any interfaces, I'd recommend you implement those on your new class also.

But, at the end of the day, C# doesn't have multiple implementation inheritance. If you need such (which is a rarity), then C# isn't an appropriate language. Nor is any other CLR language, since this is a CLR limitation, not specifically a C# one.

Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448
7

If you cannot change ClassA and ClassB I suggest creating subtypes for these classes and then aggregating.

//third party
class ClassA
{
    void X();
}
class ClassB
{
    void Y();
}
//your code
interface IClassA
{
    void X();
}
interface IClassB
{
    void Y();
}

class MyClassA : ClassA, IClassA { }
class MyClassB : ClassB, IClassB { }

class MyClass : IClassA, IClassB
{
    IClassA a = new MyClassA();
    IClassB b = new MyClassB();
    public void X()
    {
        a.X();
    }

    public void Y()
    {
        b.Y();
    }
}
Bas
  • 26,772
  • 8
  • 53
  • 86
1

No, you can't inherit from multiple classes. Only multiple interface inheritance is allowed. Consider to use composition instead:

public class MyClass : ClassA, IClassB
{
    private IClassB _classB; // delegate IClassB implementation to it

    public MyClass(IClassB classB)
    {
       _classB = classB;
    }

    public void BMethod()
    {
        _classB.BMethod();
    }
}
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
1

Create an interface for ClassA and ClassB (called IClassA and IClassB respectively) that contains all the methods you want accessible.

Then implement that interface in ClassA and ClassB.

Finally, make MyClass implement IClassA and IClassB:

public class MyClass : IClassA, IClassB
{
}
Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
  • but again I need to implement all the methods of IClassA and IClassB in MyClass. – D J Jan 11 '13 at 08:38
  • MyClass could contain an instance field of ClassA and one of ClassB. Then your implementation of IClassA and IClassB can just call the appropriate methods on the instance fields. – Matthew Watson Jan 11 '13 at 08:49
0

You could could generate an interface for ClassA and ClassB and then inherit from the 2 interfaces in MyClass

levelnis
  • 7,665
  • 6
  • 37
  • 61