I'm confused about default access modifier of C# interface members [duplicate]

陌路散爱 提交于 2019-12-05 18:08:37

Interfaces (C# Programming Guide)

Interfaces can contain methods, properties, events, indexers, or any combination of those four member types. For links to examples, see Related Sections. An interface can't contain constants, fields, operators, instance constructors, destructors, or types. Interface members are automatically public, and they can't include any access modifiers. Members also can't be static.

And about interface members implementation:

To implement an interface member, the corresponding member of the implementing class must be public, non-static, and have the same name and signature as the interface member.

So you can't implement an interface member using protected one.

Don't agree with your comment:

The CLR requires that interface methods be marked as virtual. If you do not explicitly mark the method as virtual in your source code, the compiler marks the method as virtual and sealed.

esp because you mentioned CLR Via C#, I am sure you have mis-read. Bible can not be wrong.

Edit: Providing more details.

As suggested by others, you can not have a access modifier (or virtual) in an interface member. By default (and by definition) all members inside an Interface are public and abstract.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!