1

I can't realize, how protected methods of the interface are works. I have interface and class with the protected methods: Platform - .Net Core 5

public interface ISomeInterface
{
    protected void Method_InterfaceRealization()
    {
        Console.WriteLine("JUST Inside interface realization PROTECTED");
    }

    protected void Method1();
}

public class SomeClass: ISomeInterface
{
    void ISomeInterface.Method1()
    {
        Console.WriteLine("Method_PROTECTED_NoInterfaceRealization");
    }
}
  1. How can I call Method_InterfaceRealization method outside of the interface?
  2. How can I call this Method1 anywhere? Thanks a lot!
  • 1
    https://stackoverflow.com/q/516148/11683 => https://stackoverflow.com/a/67046145/11683 => https://jeremybytes.blogspot.com/2019/11/c-8-interfaces-public-private-and.html – GSerg Sep 11 '21 at 09:00
  • 1
    @GSerg As of C#8 interfaces can have private and protected members. https://jeremybytes.blogspot.com/2019/11/c-8-interfaces-public-private-and.html – lidqy Sep 11 '21 at 09:07
  • @lidqy Did you actually read the linked to stuff, as opposed to just the title of the suggested duplicate? Because you are trying to enlighten me with the same link that I [have mentioned](https://stackoverflow.com/questions/69141480/c-sharp-interface-protected-method-how-does-it-work#comment122202234_69141480). – GSerg Sep 11 '21 at 09:14
  • @GSerg yes I read it. An I also read your comment where you said that interfaces cannot have private and protected members which is wrong. Was it you that closed the question with a wrong reason? – lidqy Sep 11 '21 at 09:30
  • @lidqy Then you didn't actually read it apart from the title of the duplicate question, like I [suggested](https://stackoverflow.com/questions/69141480/c-sharp-interface-protected-method-how-does-it-work?noredirect=1#comment122202420_69141480). My comment where "interfaces cannot have private and protected members" was the title of the duplicate question. This comment was generated automatically and it had to contain the title as it is. The title of the duplicate question is not representative because in the body of the question it is not only explained that they can, but also linked to why. – GSerg Sep 11 '21 at 10:55
  • @lidqy I see that you insist on not clicking the actual link provided in my [original comment](https://stackoverflow.com/questions/69141480/c-sharp-interface-protected-method-how-does-it-work#comment122202234_69141480). I will copy it here for you: https://stackoverflow.com/a/67046145/11683. – GSerg Sep 11 '21 at 11:21
  • @GSerg I see you insist it was a good idea, you've closed this question, though it was a bad one. The post you've linked has 15 anwsers, the accepted answer&all those highly upvoted are technically WRONG from todays C#8 perspective. 98% of SO users clicking on the link provided as closing reason will not find the hidden hint on C#8 default interface implementations. In result a productive discussion on this feature&how to use it has been prevented. And the true reason for closing this post is that you had a misconception of these the new features until you were corrected. Just deal with it ;) – lidqy Sep 11 '21 at 11:38
  • @lidqy I do not have the C# dupe hammer, so while I voted to close this question, *I* did not close it. It *was* a good idea because we dupe-close all the time when the target *contains* the correct answer, even if it's *not the accepted answer*. That is why I provided the link to the exact answer in the first place, which would have not been the case if I "had a misconception". What is *not* good is creating a bunch of questions with different accepted answers *as of year XXXX*. Feel free to update the dupe's accepted answer with a note that a more up to date answer is to be found below. – GSerg Sep 11 '21 at 11:52
  • 1
    @GSerg well okay it wasn't you but some less expierenced users will now think that there are no private / protected members in C# interfaces, it's a feature not very well known, but that can be useful if you can't change the class you derive from... but ok, everything's been said more than once now ... – lidqy Sep 11 '21 at 12:25

1 Answers1

5

As of C# 8.0, default interface implementations are permitted. Modifiers on interface members are also permitted. Protected interface members can be called from derived interfaces only, not derived classes.

You can read the original specification for the new interface-related features and some of the design discussion and decisions here:

https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-8.0/default-interface-methods

Here are two more related articles:

https://www.talkingdotnet.com/default-implementations-in-interfaces-in-c-sharp-8/

https://jeremybytes.blogspot.com/2019/11/c-8-interfaces-public-private-and.html

Neil T
  • 1,794
  • 1
  • 12
  • 21