简体   繁体   中英

Why can't I declare a public method in an interface?

Why doesn't the compiler like that I declare a public method in a class interface (this caused the error I got here .NET Class Interface, Inheritance and Library: error does not implement interface member ).

Is it just because of syntax or because of something more fundamental ?

OK it's implicit but why would it hurt to make it explicit ?

Interface is a contract and anywhere where you can access the interface, you should be able to access all the methods in it. In other words, all the methods declared in the interface are supposed to be public so it doesn't make sense stating it explicitly.

接口方法是隐式公开的,因此将它们声明为公开是多余的。

Since C# 8 things are a bit different. public modifier is now allowed. I suggest reading this article .

Check this out

Thought it is for constants, I think this is what language designers were thinking why not to permit Public in interface.

You cannot use any access modifiers in interfaces (eg private, partial). This is because it's the implementing class's responsibility to mark the accessibilty of the methods. It keeps the purpose of interfaces singular, which is to define the signature of the class, not to define its access restriction.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM