1. 程式人生 > >C# 默認訪問修飾符

C# 默認訪問修飾符

classes ike 所有 add ati always name all struct

c# 中類,成員,枚舉,結構等默認訪問修飾符是?

根據MSDN文檔有:

[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。

C# 默認訪問修飾符