存取修飾詞 (C# 程式設計手冊)Access Modifiers (C# Programming Guide)

所有類型和類型成員都具有存取範圍層級。All types and type members have an accessibility level. 存取範圍層級控制是否可以從元件或其他元件中的其他程式碼使用它們。The accessibility level controls whether they can be used from other code in your assembly or other assemblies. 當您宣告類型或成員時,請使用下列存取修飾詞來指定其存取範圍:Use the following access modifiers to specify the accessibility of a type or member when you declare it:

  • public:類型或成員可由相同元件中的任何其他程式碼或參考它的另一個元件來存取。public: The type or member can be accessed by any other code in the same assembly or another assembly that references it.
  • 用:類型或成員只能由相同或中的程式碼存取 class structprivate: The type or member can be accessed only by code in the same class or struct.
  • protected:類型或成員只能由相同 classclass 衍生自該的程式碼存取 classprotected: The type or member can be accessed only by code in the same class, or in a class that is derived from that class.
  • 內部:類型或成員可由相同元件中的任何程式碼存取,但不能由另一個元件存取。internal: The type or member can be accessed by any code in the same assembly, but not from another assembly.
  • protected internal:類型或成員可由其宣告所在元件中的任何程式碼存取,或從另一個元件中的衍生 classprotected internal: The type or member can be accessed by any code in the assembly in which it's declared, or from within a derived class in another assembly.
  • 私用保護:類型或成員只能在其宣告元件中存取,方法是使用相同 class 或衍生自該的類型中的程式碼 classprivate protected: The type or member can be accessed only within its declaring assembly, by code in the same class or in a type that is derived from that class.

下列範例示範如何指定類型和成員的存取修飾詞︰The following examples demonstrate how to specify access modifiers on a type and member:

public class Bicycle
{
    public void Pedal() { }
}

並非所有的存取修飾詞對所有內容中的所有類型或成員都有效。Not all access modifiers are valid for all types or members in all contexts. 在某些情況下,類型成員的存取範圍會受到其包含類型的存取範圍所限制。In some cases, the accessibility of a type member is constrained by the accessibility of its containing type.

類別和結構協助工具Class and struct accessibility

直接在命名空間內宣告的類別和結構(換言之,不是在其他類別或結構內)可以是 publicinternalClasses and structs declared directly within a namespace (in other words, that aren't nested within other classes or structs) can be either public or internal. Internal如果沒有指定存取修飾詞,則為預設值。Internal is the default if no access modifier is specified.

結構成員(包括嵌套類別和結構)可以宣告為 publicinternalprivateStruct members, including nested classes and structs, can be declared public, internal, or private. 類別成員(包括嵌套類別和結構)可以是 publicprotected internalprotectedinternalprivate protectedprivateClass members, including nested classes and structs, can be public, protected internal, protected, internal, private protected, or private. 類別和結構成員(包括嵌套類別和結構) private 預設具有存取權。Class and struct members, including nested classes and structs, have private access by default. 無法從包含類型外部存取私用的巢狀型別。Private nested types aren't accessible from outside the containing type.

衍生類別的存取範圍無法高於其基底類型。Derived classes can't have greater accessibility than their base types. 您無法宣告 B 衍生自內部類別的公用類別 AYou can't declare a public class B that derives from an internal class A. 如果允許,則會有 public 的效果 A ,因為的所有 protectedinternal 成員 A 都可以從衍生類別存取。If allowed, it would have the effect of making A public, because all protected or internal members of A are accessible from the derived class.

您可以使用,讓特定的其他元件存取您的內部類型 InternalsVisibleToAttributeYou can enable specific other assemblies to access your internal types by using the InternalsVisibleToAttribute. 如需詳細資訊,請參閱Friend 元件For more information, see Friend Assemblies.

類別和結構成員存取範圍Class and struct member accessibility

您可使用六種存取類型的任一種,宣告類別成員 (包括巢狀類別和結構)。Class members (including nested classes and structs) can be declared with any of the six types of access. 結構成員不能宣告為 protectedprotected internal 或, private protected 因為結構不支援繼承。Struct members can't be declared as protected, protected internal, or private protected because structs don't support inheritance.

一般來說,成員的存取範圍不會大於包含它之類型的存取範圍。Normally, the accessibility of a member isn't greater than the accessibility of the type that contains it. 不過, public 如果成員會執行介面方法或覆寫在公用基類中定義的虛擬方法,則內部類別的成員可能可以從元件外部存取。However, a public member of an internal class might be accessible from outside the assembly if the member implements interface methods or overrides virtual methods that are defined in a public base class.

任何成員欄位、屬性或事件的類型,都必須至少與成員本身一樣可以存取。The type of any member field, property, or event must be at least as accessible as the member itself. 同樣地,任何方法、索引子或委派的傳回型別和參數類型,都必須至少與成員本身一樣可以存取。Similarly, the return type and the parameter types of any method, indexer, or delegate must be at least as accessible as the member itself. 例如,除非也是,否則您不能有傳回 public M 類別的方法 C C publicFor example, you can't have a public method M that returns a class C unless C is also public. 同樣地,如果宣告為,則不能有 protected 類型的屬性 A A privateLikewise, you can't have a protected property of type A if A is declared as private.

使用者定義的運算子必須一律宣告為 publicstaticUser-defined operators must always be declared as public and static. 如需詳細資訊,請參閱運算子多載For more information, see Operator overloading.

完成項不能有存取範圍修飾詞。Finalizers can't have accessibility modifiers.

若要設定或成員的存取層級 class struct ,請將適當的關鍵字加入至成員宣告,如下列範例所示。To set the access level for a class or struct member, add the appropriate keyword to the member declaration, as shown in the following example.

// public class:
public class Tricycle
{
    // protected method:
    protected void Pedal() { }

    // private field:
    private int wheels = 3;

    // protected internal property:
    protected internal int Wheels
    {
        get { return wheels; }
    }
}

其他類型Other types

直接在命名空間內宣告的介面可以是 public 或, internal 就像類別和結構一樣,介面預設為可 internal 存取。Interfaces declared directly within a namespace can be public or internal and, just like classes and structs, interfaces default to internal access. 介面成員預設為, public 因為介面的目的是要讓其他類型可以存取類別或結構。Interface members are public by default because the purpose of an interface is to enable other types to access a class or struct. 介面成員宣告可能包含任何存取修飾詞。Interface member declarations may include any access modifier. 這最適用于靜態方法,以提供類別的所有實作者所需的一般程式。This is most useful for static methods to provide common implementations needed by all implementors of a class.

列舉成員一律為 public ,而且不能套用任何存取修飾詞。Enumeration members are always public, and no access modifiers can be applied.

委派的行為類似類別和結構。Delegates behave like classes and structs. 根據預設,它們會 internal 在命名空間中直接宣告時具有存取權,而且會 private 在 nested 時存取。By default, they have internal access when declared directly within a namespace, and private access when nested.

C# 語言規格C# language specification

如需詳細資訊,請參閱c # 語言規格For more information, see the C# Language Specification. 語言規格是 C# 語法及用法的限定來源。The language specification is the definitive source for C# syntax and usage.

另請參閱See also