Why can't I call a protected default interface method from an implementing class in C#?

I'm trying to use a protected default method in a C# interface, but I can't call it from my implementing class. For example:
interface IInterface
{
protected void Protected() { }
}
class Class : IInterface
{
public Class()
{
this.Protected(); // Fails with: 'Class' does not contain a definition for 'Protected' and no accessible extension method 'Protected' accepting a first argument of type 'Class' could be found (are you missing a using directive or an assembly reference?)
((IInterface)this).Protected(); // Fails with: Cannot access protected member 'IInterface.Protected()' via a qualifier of type 'IInterface'; the qualifier must be of type 'Class' (or derived from it)
}
}
Both direct and explicit calls fail to compile. Why is this, and how can I invoke the protected default implementation from my class? Is there a workaround or recommended pattern for this scenario? I have a situation where I can't do this as an abstract base class, and have to have this working for cases where the method is not implemented.
Answer
Interface is not a class - it cannot have implementation. On the other hand, interfaces forces class to implement that particular method.
So correct way around would be:
interface IInterface
{
void Protected();
}
class Class : IInterface
{
public Class()
{
Protected();
}
public void Protected() { }
}
Note that interface
cannot have protected void
, see i.e. https://stackoverflow.com/questions/516148/why-cant-i-have-protected-interface-members for details.
----
But instead of interface
, you can use inheritance from class:
public class ABaseClass
{
protected void Protected() { }
}
class Class : ABaseClass
{
public Class()
{
this.Protected();
}
}