开发者

Why do interface members have no access modifier? [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Why can't I have protected interface members?

as title, in C#. Is there no possibility that 开发者_JAVA百科someone might want to have a protected or an internal interface?


Because Interface is in crude terms 'a view to the outside world' and since it is for the outside world, there is no point making its members protected or private.

Or in other words, it is a contract with the outside world which specifies that class implementing this interface does a certain set of things. So, hiding some part of it doesn't make sense.

However, interfaces themselves can have access specifiers like protected or internal etc. Thus limiting 'the outside world' to a subset of 'the whole outside world'.


Interface members are always public because the purpose of an interface is to enable other types to access a class or struct. No access modifiers can be applied to interface members.


All the interface methods are Public. You can't create an access modifier in interface. If you want to use one, use Abstract class.


This is due to the nature of the interface. An interface, by definition is a specification. A rule in .NET specifications dictates that a class that implements an interface will have to implement all members of that interface. Now if we mark a member private, then the implementing class cannot implement that particular member.


Please see Non Public Members for C# Interfaces


Interfaces are Coding contracts, this is the very reason it won't allow any access modifier other then Public in it's Method signatures. But an Interface by itself can be Internal but not private or protected, Internal allows access within the assembly which is perfectly fine.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜