user310291
user310291

Reputation: 38180

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 ?

Upvotes: 31

Views: 25578

Answers (5)

Anton
Anton

Reputation: 2636

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

Upvotes: 21

Sandeep
Sandeep

Reputation: 7334

Check this out

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

Upvotes: 3

Morgan T.
Morgan T.

Reputation: 1937

You cannot use any access modifiers in interfaces (e.g. 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.

Upvotes: 1

bitxwise
bitxwise

Reputation: 3594

Interface methods are implicitly public so declaring them public is redundant.

Upvotes: 8

Bala R
Bala R

Reputation: 108957

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.

Upvotes: 57

Related Questions