inherit from two classes in C#

.net, c#

Solution

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

The basic idea is to define an interface for the members on class `B` that you wish to access (call it `IB`), and then have `C` inherit from `A` and implement `IB` by internally storing 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 alternaitve patterns explained on that page.

Problem

Possible Duplicate: Multiple Inheritance in C# I have two classes Class A and Class B. These two classes cannot inherit each other. I am creating new class called Class C. Now, I want to implement the methods in class A and class B by inheriting. I am aware that multiple inheritance is not possible in C# but is there any other way to do this?

Original source

Related problems