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

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

publicpublic
類型或成員可由相同組件或參考該組件的另一個組件中的任何其他程式碼存取。The type or member can be accessed by any other code in the same assembly or another assembly that references it.

privateprivate
類型或成員只能由相同類別或結構中的程式碼進行存取。The type or member can be accessed only by code in the same class or struct.

protectedprotected
類型或成員只可由相同類別或衍生自該類別之類別中的程式碼進行存取。The type or member can be accessed only by code in the same class, or in a class that is derived from that class.
internalinternal
類型或成員可由相同組件中的任何程式碼存取,但是不包括其他組件中的程式碼。The type or member can be accessed by any code in the same assembly, but not from another assembly.

protected internal 類型或成員可由宣告程式碼的組件內之任一程式碼存取,或是從另一個組件中的衍生類別內存取。protected internal The type or member can be accessed by any code in the assembly in which it is declared, or from within a derived class in another assembly.

private protected 類型或成員只可從其宣告組件內存取,或是從衍生自該類別之相同類別或類型的程式碼存取。private 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 can be used by all types or members in all contexts, and in some cases the accessibility of a type member is constrained by the accessibility of its containing type. 下列各節提供存取範圍的詳細資料。The following sections provide more details about accessibility.

類別和結構存取範圍Class and Struct Accessibility

直接在命名空間內宣告的類別和結構 (換句話說,未巢狀在其他類別或結構內) 可以是 public 或 internal。Classes and structs that are declared directly within a namespace (in other words, that are not nested within other classes or structs) can be either public or internal. 如果未指定任何存取修飾詞,則 internal 是預設值。Internal is the default if no access modifier is specified.

結構成員 (包括巢狀類別和結構) 可以宣告為 public、internal 或 private。Struct members, including nested classes and structs, can be declared as public, internal, or private. 類別成員,包含巢狀類別和結構,可為 public、protected internal、protected、internal、private protected 或 private。Class members, including nested classes and structs, can be public, protected internal, protected, internal, private protected or private. 類別成員和結構成員 (包括巢狀類別和結構) 的存取層級預設為 private。The access level for class members and struct members, including nested classes and structs, is private by default. 無法從包含類型外部存取私用巢狀型別。Private nested types are not accessible from outside the containing type.

衍生類別的存取範圍不能大於其基底類型。Derived classes cannot have greater accessibility than their base types. 換句話說,您不能有衍生自內部類別 A 的公用類別 BIn other words, you cannot have a public class B that derives from an internal class A. 因為 A 的所有 protected 或 internal 成員都可以從衍生類別進行存取,所以如果允許這樣做,則會有將 A 設為 public 的效果。If this were allowed, it would have the effect of making A public, because all protected or internal members of A are accessible from the derived class.

您可以使用 InternalsVisibleToAttribute 來啟用特定其他組件存取您的內部類型。You can enable specific other assemblies to access your internal types by using the InternalsVisibleToAttribute. 如需詳細資訊,請參閱 Friend Assemblies (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. 因為結構不支援繼承,所以無法將結構成員宣告為 protected。Struct members cannot be declared as protected because structs do not support inheritance.

一般而言,成員存取範圍不會大於包含它之類型的存取範圍。Normally, the accessibility of a member is not greater than the accessibility of the type that contains it. 不過,如果成員實作介面方法,或覆寫公用基底類別中所定義的虛擬方法,則可以從組件外部存取內部類別的公用成員。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 that is a field, property, or event must be at least as accessible as the member itself. 同樣地,傳回型別以及本身為方法、索引子或委派之任何成員的參數類型都必須至少像該成員本身一樣可供存取。Similarly, the return type and the parameter types of any member that is a method, indexer, or delegate must be at least as accessible as the member itself. 例如,除非 C 也是公用的,否則您無法有傳回 C 類別的公用方法 MFor example, you cannot have a public method M that returns a class C unless C is also public. 同樣地,如果將 A 宣告為 private,則您不能有 A 類型的 protected 屬性。Likewise, you cannot have a protected property of type A if A is declared as private.

使用者定義的運算子一律必須宣告為 public。User-defined operators must always be declared as public. 如需詳細資訊,請參閱 operator (C# 參考)For more information, see operator (C# Reference).

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

若要設定類別或結構成員的存取層級,請在成員宣告中新增適當的關鍵字,如下列範例所示。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; }
    }
}

注意

protected internal 存取範圍層級表示「protected 或 internal」,而非「protected 和 internal」。The protected internal accessibility level means protected OR internal, not protected AND internal. 換句話說,可以從相同組件的任何類別 (包括衍生類別) 中存取 protected internal 成員。In other words, a protected internal member can be accessed from any class in the same assembly, including derived classes. 若要僅限於相同組件中衍生類別的存取範圍,請將類別本身宣告為 internal,並將它的成員宣告為 protected。To limit accessibility to only derived classes in the same assembly, declare the class itself internal, and declare its members as protected. 此外,從 C# 7.2 開始,您可使用 private protected 存取修飾詞來達到相同的結果,而無須將包含的類別設為 internal。Also, starting with C# 7.2, you can use the private protected access modifier to achieve the same result without need to make the containing class internal.

其他類型Other Types

直接在命名空間內宣告的介面可以宣告為 public 或 internal;而且,就像類別和結構,介面預設為內部存取。Interfaces declared directly within a namespace can be declared as public or internal and, just like classes and structs, interfaces default to internal access. 介面成員一律都是 public,因為介面的目的是要讓其他類型存取類別或結構。Interface members are always public because the purpose of an interface is to enable other types to access a class or struct. 沒有存取修飾詞可以套用至介面成員。No access modifiers can be applied to interface members.

列舉成員一律為 public,因此無法套用任何存取修飾詞。Enumeration members are always public, and no access modifiers can be applied.

委派的行為類似類別和結構。Delegates behave like classes and structs. 根據預設,它們在直接在命名空間內宣告時會具有 internal 存取,而且在巢狀時會具有 private 存取。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