Multiple Inheritance with C#

28Aug06

Some time ago I encountered a problem in C# regarding multiple inheritance. Actually this is more a problem of .NET, because the framework specification does not support multiple inheritance.
Anyway, let’s assume we have class A, B, C that inherit from Control. These classes also implement a bunch of interfaces and share basic implementation of some of the interface methods.
There are different solutions possible:
(1) The obvious solution is not allowed: Derive classes A, B and C from Control and AbstractBase.
(2) Define an abstract class AbstractBase, which derives from Control and implements the ‘shared’ methods and defines some abstract methods that differ from derived class to derived class.
(3) Implement a pattern to simulate multiple inheritance: see ‘Twin-Pattern’
(4) Derive your classes from Control as described above and add a private field with an instance of type AbstractClass. All ‘shared’ methods will be called through the instance.

There are pros and contras for each solution. A forthcoming article will go into more details.



2 Responses to “Multiple Inheritance with C#”

  1. I like the different solutions.I could think of 2nd solution you mentioned. 4th solution seems interesting and i am gonig to give it a try.

    Keep on posting this type of articles. They are a really good read.

    Cheers
    Ankit

  2. This is a topic that is close to my heart… Take care!

    Exactly where are your contact details though?


Leave a comment