หมายเหตุ
การเข้าถึงหน้านี้ต้องได้รับการอนุญาต คุณสามารถลอง ลงชื่อเข้าใช้หรือเปลี่ยนไดเรกทอรีได้
การเข้าถึงหน้านี้ต้องได้รับการอนุญาต คุณสามารถลองเปลี่ยนไดเรกทอรีได้
The accessibility domain of a member specifies the program sections where you can reference that member. If the member is nested within another type, both the accessibility level of the member and the accessibility domain of the immediately containing type determine its accessibility domain.
The C# language reference documents the most recently released version of the C# language. It also contains initial documentation for features in public previews for the upcoming language release.
The documentation identifies any feature first introduced in the last three versions of the language or in current public previews.
Tip
To find when a feature was first introduced in C#, consult the article on the C# language version history.
The accessibility domain of a top-level type always includes at least the program text of the project where you declare it. In other words, the domain includes all source files in the project. The accessibility domain of a nested type always includes at least the program text of the type where you declare it. In other words, 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. The following example demonstrates these concepts.
This example contains a top-level type, T1, and two nested classes, M1 and M2. The classes contain fields that have different declared accessibilities. In the Main method, a comment follows each statement to indicate the accessibility domain of each member. 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# Language Specification
For more information, see the C# Language Specification. The language specification is the definitive source for C# syntax and usage.