Sezi
Sezi

Reputation: 93

Explicit Implementation of Marker C# Interfaces

Imagine a situation where two marker interfaces A and B guard single Base interface:

interface Base
{
    void Hello();
}

interface A: Base
{

}

interface B: Base
{

}

Is there a way to define class C that implements both A and B while providing marker interface specific Hello implementations? Note that Base, A and B interfaces are declared in an external library so I could not modify it.

If I do this

class C: A, B
{
    void A.Hello() =>
        Console.Out.WriteLine("A");

    void B.Hello() =>
        Console.Out.WriteLine("B");
}

...it results in CS0539 compiler error:

member in explicit interface declaration is not a member of interface.

Upvotes: 2

Views: 98

Answers (1)

knittl
knittl

Reputation: 265251

No, it's not possible in C# without modifying the interfaces. The only explicit interface implementation that is possible is Base.Hello:

class C: A, B
{
    void Base.Hello() =>
        Console.Out.WriteLine("Base");
}

Upvotes: 2

Related Questions