Search code examples
c#inheritanceinterfaceabstract-class

Is diamond problem with interfaces in C# possible?


Are there architecture problems in the code below? Is the so called diamond problem possible with interfaces or similar issues?

interface IComponent
{
    void DoStuff();
}

interface ITitledComponent : IComponent
{
    string Title { get; }
}

abstract class ComponentBase : IComponent
{
    public void DoStuff()
    {
        throw new NotImplementedException();
    }
}

class MyComponent : ComponentBase, ITitledComponent
{
    public string Title => throw new NotImplementedException();
}

Certainly, diamond inheritance with classes is a bad decision and this is not possible in C#. But about the interfaces I did not find information.


Solution

  • No, it's not possible to produce a diamond problem with C#, because you can only ever inherit from one class. Interfaces are not inherited, but implemented. So the actual problem for the compiler and coder, having two implementations of a method and not knowing which to pick for a specific class can never happen.