访问修饰符


类(class)或结构(struct)如果不是在其它类或结构中的话,它的访问类型要不就是internal, 要不就是public; 
换句话说,如果它在其它类或结构中的话,则可以为private 或protected等。下面我说的类和结构,如无特殊说明,均指非"类中类" 类中所有的成员,默认均为private。
C#用多种修饰符来表达类的不同性质。根据其保护级C#的类有五种不同的限制修饰符: public可以被任意存取; protected只可以被本类和其继承子类存取;
internal只可以被本组合体(Assembly)内所有的类存取,组合体是C#语言中类被组合后的逻辑单位和物理单位,其编译后的文件扩展名往往是“.DLL”或“.EXE”。
protected internal唯一的一种组合限制修饰符,它只可以被本组合体内所有的类和这些类的继承子类所存取。 private只可以被本类所存取。
如果不是嵌套的类,命名空间或编译单元内的类只有public和internal两种修饰。 new修饰符只能用于嵌套的类,表示对继承父类同名类型的隐藏。
override 只能用于嵌套的类,表示对继承父类同名类型的覆盖。 abstract用来修饰抽象类,表示该类只能作为父类被用于继承,而不能进行对象实例化。抽象类可以包含抽象的成员,但这并非必须。abstract不能和new同时用。下面是抽象类用法的伪码:
abstract class A { public abstract void F(); } abstract class B: A { public void G() {} } class C: B { public override void F()
{ //方法F的实现 } } 抽象类A内含一个抽象方法F(),它不能被实例化。类B继承自类A,其内包含了一个实例方法G(),但并没有实现抽象方法F(),所以仍然必须声明为抽象类。类C继承自类B,实现类抽象方法F(),于是可以进行对象实例化。
sealed用来修饰类为密封类,阻止该类被继承。同时对一个类作abstract和sealed的修饰是没有意义的,也是被禁止的。
enum 的默认访问修饰符:public,且此类型不允许其它访问修饰符interface的默认访问修饰符:interal,且此类型不允许其它访问修饰符;接口的成员默认访问修饰符是public,也不可能是其他访问修饰符 委托,默认internal 命名空间上不允许使用访问修饰符。命名空间没有访问限制。
(命名空间,枚举类型成员默认public,也不可能是其他访问修饰符 //?????? 待验证) c# 的访问修饰符是private 还是 internal? 准确的说,不能一概而论。 [MSDN] Classes and structs that are not nested within other classes or structs can be either public or internal.
A type declared as public is accessible by any other type. A type declared as internal is only accessible by types within the same assembly. Classes and structs are declared as internal by

default unless the keyword public is added to the class definition, as in the previous example. Class or struct definitions can add the internal keyword to make their access level explicit. Access modifiers do not affect the class or struct itself —
it always has access to itself and all of its own members. 类(
class)或结构(struct)如果不是在其它类或结构中的话,它的访问类型要不就是internal, 要不就是public; 换句话说,如果它在其它类或结构中的话,则可以为private 或protected等。下面我说的类和结构,如无特殊说明,均指非"类中类" 类或结构的默认访问类型是internal. 类中所有的成员,默认均为private。 [MSDN] Interfaces, like classes, can be declared as public or internal types. Unlike classes, interfaces default to internal access. Interface members are always public, and no access modifiers can be applied. Namespaces and enumeration members are always public,
and no access modifiers can be applied. Delegates have internal access by default. Any types declared within a namespace or at the top level of a compilation unit (for example, not within a namespace, class, or struct) are internal by default, but can be made public. 接口默认访问符是internal 接口的成员默认访问修饰符是public,也不可能是其他访问修饰符 命名空间,枚举类型成员默认public,也不可能是其他访问修饰符 委托,默认internal 在命名空间内部或编译单元顶部的所有类型,默认是internal,可以人为改为public。
菜鸟-潜水中
原文地址:https://www.cnblogs.com/C-CHERS/p/4279777.html