0

I came across this problem when i was implementing n interface explicitly using Visual Studio. So the interface contains properties, but when I am implementing the property explicitly in an abstract class, Compiler throws error "The modifier 'public' is not valid for this item".

Refer Below given code.

interface ITest
{
    bool MyProperty { get; set; }
}

internal class Test : ITest
{
    public bool ITest.MyProperty
    {
        get
        {
            return false;
        }    

        set { }
    }
}
Rahul
  • 45
  • 6

1 Answers1

0

According to the programming guide, explicit interface implementations always lack an access modifier. You should remove the public keyword.

If you think about it, this makes a lot of sense. There is only one possible access modifier for an explicit interface implementation - the same modifier used for the interface. Thus, you don't need to specify the modifier.

If the interface is marked public, and the explicit implementation is private, that will not make sense. The only reason to write an explicit implementation is to expose that member to only that interface. It would be weird if the member is less accessible than the interface, right?

On the other hand, if the interface is internal and the member is marked public, it will not make sense either. If the member is more accessible than the interface, then it will not be exposed only to the interface.

Sweeper
  • 213,210
  • 22
  • 193
  • 313