1

How to use protected property from interface in derived class c#?

There is interface:

    interface I1
    {
        protected delegate void MoveHandler(string message);
        protected string Name => "hi";
    }
    

I tried take it from interface:

    class A : I1
    {
        public void doWork()
        {
            I1.MoveHandler i = new I1.MoveHandler(() => { });
            // but no I1.Name
        }
    }

I tried take from this class context:

    class A : I1
    {
        public void doWork()
        {
            Console.WriteLine(Name);
        }
    }

But there is no success. How to use them?

John
  • 164
  • 9
  • Does this answer your question? [Calling C# interface default method from implementing class](https://stackoverflow.com/questions/57761799/calling-c-sharp-interface-default-method-from-implementing-class) – Progman Jun 27 '20 at 16:24
  • It seems that protected default implementations aren't accesible. Default implementations aren't completely implemented [according to MSDN](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-8.0/default-interface-methods), so I would avoid it's usage. – Gusman Jun 27 '20 at 16:29
  • Progman, possibly no (compilation error). – John Jun 27 '20 at 16:29
  • Gusman, this link about proposal for c#, it is not documentation of c# 8. or not? – John Jun 27 '20 at 17:07

1 Answers1

1

Not an answer to your question, but it seems kind of interesting. While it seems impossible to do something useful with protected member in implementing class (also you can explicitly implement it yourself, but that's it, you still can't do anything with it):

class A : I1
{
    public void doWork()
    {
        I1.MoveHandler i = new I1.MoveHandler((i) => { });
        // but no I1.Name
    }
    
    string I1.Name => "A";
}

You can use it in derived interface:

interface I2 : I1
{
    string NameI2 => Name;
}

And the NameI2 can be used:

class A2 : I2
{
}

I2 i2 = new A2();
Console.WriteLine(i2.NameI2); // prints "hi"

Or even overloaded in implementing class:

class A2 : I2
{
    string I1.Name => "InA2";
}
I2 i2 = new A2();
Console.WriteLine(i2.NameI2); // prints "InA2"

I have not found a lot of documentation about meaning protected modifier interface members. There is some on Roslyn github page about default interface implementation, but I was not ale to decipher it)

UPD

Found some kind of useful usage - since it can be overridden, it can be used as some kind of template pattern:

interface I1
{
    public string Name { get; }
    public void SayHello() => Console.WriteLine($"{Greeting}, {Name}");
    protected string Greeting => "hi";
}

class A : I1
{
    public string Name => "Slim Shady";
    string I1.Greeting => "Yo";
}

I1 a = new A();
a.SayHello(); // prints "Yo, Slim Shady"
Guru Stron
  • 102,774
  • 10
  • 95
  • 132
  • 1
    DIM essentially enabled all access modifiers in an interface. Your unlikely to find documentation about a *specific* modifier since they're all "valid" (though potentially inaccessible in practice) – pinkfloydx33 Jun 27 '20 at 16:58