In C #, you don't have multiple inheritance. You can get around this limitation using composition .
Define your interface as follows ( Function does not need to be defined here):
public interface IMyInterface { void MyAction(); }
Declare an abstract class with abstract Function and implement this interface:
public abstract class MyInterfaceBase : IMyInterface { public void MyAction() {
From this abstract class you can get a concrete implementation. This is not your βfinalβ class, but it will be used to create it.
public class ConcreteMyInterface : MyInterfaceBase { protected override void Function() { Console.WriteLine("hello"); } }
Now let me come to your "final", compiled class. It will be obtained from SomeBaseClass and implements IMyInterface by integrating the ConcreteMyInterface functionality:
public class SomeBaseClass { } public class MyComposedClass : SomeBaseClass, IMyInterface { private readonly IMyInterface _myInterface = new ConcreteMyInterface(); public void MyAction() { _myInterface.MyAction(); } }
UPDATE
In C #, you can declare local classes. This is approaching multiple inheritance, as you can get everything in your composition.
public class MyComposedClass : SomeBaseClass, IMyInterface { private readonly IMyInterface _myInterface = new ConcreteMyInterface(); public void MyAction() { _myInterface.MyAction(); } private class ConcreteMyInterface : MyInterfaceBase { protected override void Function() { Console.WriteLine("hello"); } } }
Olivier Jacot-Descombes
source share