Search code examples
c#interfaceroslynprotected

What version of C# stopped requiring protected interface members to be implemented explicitly?


In C# 8, interfaces were upgraded to allow protected members, among other access modifiers. At the time, implementing classes were required to explicitly implement such protected interface members.

So, under C# 8 this code:

public interface IFoo
{
    protected void Method();
}

public class Foo : IFoo
{
    public void Method()
    {
    }
}

produces this compilation error: CS8704 'Foo' does not implement interface member 'IFoo.Method()'. 'Foo.Method()' cannot implicitly implement a non-public member.

I hadn't written any code that took advantage of this "feature" (force the hiding of implementer specific helper methods from normal public access) lately, but recently I noticed that the explicit implementation is no longer required. The code snippet above now compiles without issue (as of C# 11 at least). Why/when did this change? I couldn't find any documentation or discussion of the change, did it happen incidentally due to other language changes?


Solution

  • In C#8, the decision was made to only allow explicit implementations. The available options were to restrict how protected could be used, to simplify compatibility with future language changes. Or prevent protected from being used at all.

    This restriction was later relaxed. I would consider both the initial implementation and later changes to the roslyn compiler to be "undefined behaviour". Since I don't believe that the C# language specification defines what the behaviour should be.