存取範圍定義域 (C# 參考)Accessibility Domain (C# Reference)

成員的存取範圍定義域指定可參考成員的程式區段。The accessibility domain of a member specifies in which program sections a member can be referenced. 如果成員巢狀在另一個類型內,則其存取範圍定義域是由成員的存取範圍層級和立即包含類型的存取範圍定義域所決定。If the member is nested within another type, its accessibility domain is determined by both the accessibility level of the member and the accessibility domain of the immediately containing type.

最上層類型的存取範圍定義域是至少在其中宣告之專案的程式文字。The accessibility domain of a top-level type is at least the program text of the project that it is declared in. 亦即,定義域包括此專案的所有原始程式檔。That is, the domain includes all of the source files of this project. 巢狀型別的存取範圍定義域是至少在其中宣告之類型的程式文字。The accessibility domain of a nested type is at least the program text of the type in which it is declared. 亦即,定義域是包括所有巢狀型別的類型主體。That is, the domain is the type body, which includes all nested types. 巢狀型別的存取範圍定義域絕不會超過包含類型的存取範圍定義域。The accessibility domain of a nested type never exceeds that of the containing type. 下列範例示範這些概念。These concepts are demonstrated in the following example.

範例Example

這個範例包含最上層類型 T1 以及兩個巢狀類別 M1M2This example contains a top-level type, T1, and two nested classes, M1 and M2. 這些類別包含具有不同已宣告存取範圍的欄位。The classes contain fields that have different declared accessibilities. Main 方法中,註解在每個陳述式的後面,表示每個成員的存取領域。In the Main method, a comment follows each statement to indicate the accessibility domain of each member. 請注意,會註解化嘗試參考無法存取成員的陳述式。如果您想要查看因參考無法存取的成員而造成的編譯器錯誤,請一次移除一個註解。Notice that the statements that try to reference the inaccessible members are commented out. If you want to see the compiler errors caused by referencing an inaccessible member, remove the comments one at a time.

public class T1
{
    public static int publicInt;
    internal static int internalInt;
    private static int privateInt = 0;
    static T1()
    {
        // T1 can access public or internal members
        // in a public or private (or internal) nested class.
        M1.publicInt = 1;
        M1.internalInt = 2;
        M2.publicInt = 3;
        M2.internalInt = 4;

        // Cannot access the private member privateInt
        // in either class:
        // M1.privateInt = 2; //CS0122
    }

    public class M1
    {
        public static int publicInt;
        internal static int internalInt;
        private static int privateInt = 0;
    }

    private class M2
    {
        public static int publicInt = 0;
        internal static int internalInt = 0;
        private static int privateInt = 0;
    }
}

class MainClass
{
    static void Main()
    {
        // Access is unlimited.
        T1.publicInt = 1;

        // Accessible only in current assembly.
        T1.internalInt = 2;

        // Error CS0122: inaccessible outside T1.
        // T1.privateInt = 3;  

        // Access is unlimited.
        T1.M1.publicInt = 1;

        // Accessible only in current assembly.
        T1.M1.internalInt = 2;

        // Error CS0122: inaccessible outside M1.
        //    T1.M1.privateInt = 3; 

        // Error CS0122: inaccessible outside T1.
        //    T1.M2.publicInt = 1;

        // Error CS0122: inaccessible outside T1.
        //    T1.M2.internalInt = 2;

        // Error CS0122: inaccessible outside M2.
        //    T1.M2.privateInt = 3;

        // Keep the console open in debug mode.
        System.Console.WriteLine("Press any key to exit.");
        System.Console.ReadKey();
    }
}

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