C# does not directly provide such a pattern since multiple inheritance is bad (it makes the code more difficult). However, having this skill would be useful at times.
For example, I can use interfaces and three classes to implement the missing multiple inheritance pattern:
public interface IFirst { void FirstMethod(); }
public interface ISecond { void SecondMethod(); }
public class First:IFirst
{
public void FirstMethod() { Console.WriteLine("First"); }
}
public class Second:ISecond
{
public void SecondMethod() { Console.WriteLine("Second"); }
}
public class FirstAndSecond: IFirst, ISecond
{
First first = new First();
Second second = new Second();
public void FirstMethod() { first.FirstMethod(); }
public void SecondMethod() { second.SecondMethod(); }
}
I have to update the class FirstAndSecond every time I add a method to one of the interfaces.
Is it possible, as in C++, to inject numerous existing classes into a single new class?
Is there a way to solve this problem with code generation?
Alternatively, it may look something like this (imaginary c# syntax):
public class FirstAndSecond: IFirst from First, ISecond from Second
{ }