Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 645
R. Savelyev Avatar asked Feb 03 '26 16:02

R. Savelyev


1 Answers

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.

like image 90
nvoigt Avatar answered Feb 06 '26 05:02

nvoigt