C#修饰符说明
方法不加訪問修飾符默認的是 private
類不加訪問修飾答默認的是 internal
/
一個編譯器錯誤提示:在命名空間中定義的元素?zé)o法顯示的聲明為 private, protected, protected internal。
能在命名空間中定義的元素有:類(class),結(jié)構(gòu)(struct),委托(delegate),接口(interface),枚舉(enum)
MSDN提示: protected internal 可訪問性的意思是受保護“或”內(nèi)部,而不是受保護“和”內(nèi)部。
我們的訪問修飾符一共有5個級別
public, private, protected, internal, protected internal1.命名空間下的元素的默認訪問修飾符
根據(jù)上面的紅色錯誤提示,可以知道命名空間下只能使用兩種訪問修飾符public和internal。如果沒有顯示的給這些元素訪問修飾符,其修飾符默認為internal 。
public : 同一程序集的其他任何代碼或引用該程序集的其他程序集都可以訪問該類型或成員。
internal : 同一程序集中的任何代碼都可以訪問該類型或成員,但其他程序集不可以訪問。 2.各類型中的成員的默認訪問修飾符
剩下的修飾符主要是正對繼承這個語言特性的,擁有繼承的類型有兩個類(class)和接口(interface)。public,internal同樣可以用于類型成員。
private : 同一類和結(jié)構(gòu)的代碼可以訪問該類型和成員。
protected : 同一類和派生(繼承特性)類中的代碼可以訪問該類型和成員。
protected internal : 同一程序集中的任何代碼或其他程序集中的任何派生類都可以訪問該類型或成員。MSDN提示 :
1.) 派生類的可訪問性不能高于其基類型。換句話說,不能有從內(nèi)部類 A 派生的公共類 B。如果允許這種情況,將會使 A 成為公共類,因為 A 的所有受保護的成員或內(nèi)部成員都可以從派生類訪問。
2.) 成員的可訪問性決不能高于其包含類型的可訪問性。
3.) 可以使用五種訪問類型中的任何一種來聲明類成員(包括嵌套的類和結(jié)構(gòu))。 接口(interface)
接口成員訪問修飾符默認為public,且不能顯示使用訪問修飾符。類(class)
構(gòu)造函數(shù)默認為public訪問修飾符。
析構(gòu)函數(shù)不能顯示使用訪問修飾符且默認為private訪問修飾符。
類的成員默認訪問修飾符為private; 枚舉(enum)
枚舉類型成員默認為public訪問修飾符,且不能顯示使用修飾符。結(jié)構(gòu)(struct)
結(jié)構(gòu)成員默認為private修飾符。
結(jié)構(gòu)成員無法聲明為protected成員,因為結(jié)構(gòu)不支持繼承。 嵌套類型
嵌套類型的默認訪問修飾符為private。 和類,結(jié)構(gòu)的成員默認訪問類型一致。
///
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)或結(jié)構(gòu)(struct)如果不是在其它類或結(jié)構(gòu)中的話,它的訪問類型要不就是internal, 要不就是public;
換句話說,如果它在其它類或結(jié)構(gòu)中的話,則可以為private 或protected等。下面我說的類和結(jié)構(gòu),如無特殊說明,均指非"類中類"
類或結(jié)構(gòu)的默認訪問類型是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
在命名空間內(nèi)部或編譯單元頂部的所有類型,默認是internal,可以人為改為public。
轉(zhuǎn)載于:https://www.cnblogs.com/XuPengLB/p/6382546.html
總結(jié)
- 上一篇: 每天一个 Linux 命令(13):le
- 下一篇: php面向对象(OOP)---- 验证码