访问修饰符(比较好的文章,转帖)

 

C#访问修饰符(Access Modifiers)
作者:AY20080808 提交日期:2009-3-23 15:51:00 正常 | 分类: | 访问量:34


 

访问修饰符是一些关键字,用于指定声明的成员或类型的可访问性。这些关键字包括 public、private、protected 和 internal。


一、可访问性级别(Accessibility Levels)


使用这些访问修饰符可指定下列五个可访问性级别(Accessibility Levels):
◆public     Access is not restricted.
             访问不受限制。
◆protected  Access is limited to the containing class or types derived from the containing class.
             访问范围限定于它所属的类或从该类派生的类型。
◆internal   Access is limited to the current assembly.
             访问范围限定于此程序。
◆protected  internal   Access is limited to the current assembly or types derived from the containing class.
             访问范围限定于此程序或那些由它所属的类派生的类型。
◆private    Access is limited to the containing type.
             访问范围限定于它所属的类型。


Only one access modifier is allowed for a member or type, except for the protected internal combination.


Access modifiers are not allowed on namespaces. Namespaces have no access restrictions.


Depending on the context in which a member declaration takes place, only certain declared accessibilities are permitted. If no access modifier is specified in a member declaration, a default accessibility is used.


Top-level types, which are not nested into other types, can only have internal or public accessibility. The default accessibility for these types is internal.


Nested types, which are members of other types, can have declared accessibilities as indicated in the following table.
----------------------------------------------------------------------
Members of   |   Default member   |   Allowed declared accessibility
             |   accessibility    |   of the member
----------------------------------------------------------------------
enum         |   public           |   None
----------------------------------------------------------------------
class        |   private          |   public
             |                    |   protected
             |                    |   internal
             |                    |   private
             |                    |   protected internal
----------------------------------------------------------------------
interface    |   public           |   None
----------------------------------------------------------------------
struct       |   private          |   public
             |                    |   internal
             |                    |   private
----------------------------------------------------------------------
The accessibility of a nested type depends on its accessibility domain, which is determined by both the declared accessibility of the member and the accessibility domain of the immediately containing type. However, the accessibility domain of a nested type cannot exceed that of the containing type.


二、成员访问


声明一个成员时所能选择的可访问性的类型,依赖于该成员声明出现处的上下文。此外,当成员声明不包含任何访问修饰符时,声明发生处的上下文会为该成员选择一个默认的可访问性。


◆命名空间隐式地具有 public 已声明可访问性。在命名空间声明中不允许使用访问修饰符。
编译单元或命名空间中声明的类型可以具有 public 或 internal 已声明可访问性,默认的已声明可访问性为 internal。
◆类成员可具有五种已声明可访问性中的任何一种,默认为 private 已声明可访问性。(请注意,声明为类成员的类型可具有五种已声明可访问性中的任何一种,而声明为命名空间成员的类型只能具有 public 或 internal 已声明可访问性。)
◆结构成员可以具有 public、internal 或 private 已声明可访问性并默认为 private 已声明可访问性,这是因为结构是隐式地密封的。结构的成员若是在此结构中声明的(也就是说,不是由该结构从它的基类中继承的)不能具有 protected 或 protected internal 已声明可访问性。(请注意,声明为结构成员的类型可具有 public、internal 或 private 已声明可访问性,而声明为命名空间成员的类型只能具有 public 或 internal 已声明可访问性。)
◆接口成员隐式地具有 public 已声明可访问性。在接口成员声明中不允许使用访问修饰符。
◆枚举成员隐式地具有 public 已声明可访问性。在枚举成员声明中不允许使用访问修饰符。

 

原文地址:https://www.cnblogs.com/zhangjun1130/p/1574994.html