ECMA-334: 10.5.2 Accessibility domains
Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio
| C# Language Specification |
| © 2006 ECMA International |
10.5.2 Accessibility domains
The accessibility domain of a member consists of the (possibly disjoint) sections of program text in which
access to the member is permitted. For purposes of defining the accessibility domain of a member, a member
is said to be top-level if it is not declared within a type, and a member is said to be nested if it is declared
within another type. Furthermore, the text of a program is defined as all source text contained in all source
files of that program, and the source text of a type is defined as all source text contained between the
opening and closing "{" and "}" tokens in the class-body, struct-body, interface-body, or enum-body of all
declarations for the type (including, possibly, multiple partial declarations and all types that are nested
within the type).
The accessibility domain of a predefined type (such as object, int, or double) is unlimited.
The accessibility domain of a top-level type T that is declared in a program P is defined as follows:
- If the declared accessibility of
Tispublic, the accessibility domain ofTis the program text ofPand any program that referencesP.
- If the declared accessibility of
Tisinternal, the accessibility domain ofTis the program text ofP. [Note: From these definitions, it follows that the accessibility domain of a top-level type is always at least the program text of the program in which that type is declared. end note] The accessibility domain of a nested memberMdeclared in a typeTwithin a programP, is defined as follows (noting thatMitself might possibly be a type):
- If the declared accessibility of
Mispublic, the accessibility domain ofMis the accessibility domain ofT.
- If the declared accessibility of
Misprotected internal, letDbe the union of the program text ofPand the program text of any type derived fromT, which is declared outsideP. The accessibility domain ofMis the intersection of the accessibility domain ofTwithD.
- If the declared accessibility of
Misprotected, letDbe the union of the program text ofTand the program text of any type derived fromT. The accessibility domain ofMis the intersection of the accessibility domain ofTwithD.
- If the declared accessibility of
Misinternal, the accessibility domain ofMis the intersection of the accessibility domain ofTwith the program text ofP.
- If the declared accessibility of
Misprivate, the accessibility domain ofMis the program text ofT.
[Note: From these definitions, it follows that the accessibility domain of a nested member is always at least the program text of the type in which the member is declared. Furthermore, it follows that the accessibility domain of a member is never more inclusive than the accessibility domain of the type in which the member is declared. end note]
[Note: In intuitive terms, when a type or member M is accessed, the following steps are evaluated to ensure that the access is permitted:
- First, if
Mis declared within a type (as opposed to a compilation unit or a namespace), a compile-time error occurs if that type is not accessible.
- Then, if
Mispublic, the access is permitted.
- Otherwise, if
Misprotected internal, the access is permitted if it occurs within the program in whichMis declared, or if it occurs within a class derived from the class in whichMis declared and takes place through the derived class type (§10.5.3).
- Otherwise, if
Misprotected, the access is permitted if it occurs within the class in whichMis declared, or if it occurs within a class derived from the class in whichMis declared and takes place through the derived class type (§10.5.3).
- Otherwise, if
Misinternal, the access is permitted if it occurs within the program in whichMis declared.
- Otherwise, if
Misprivate, the access is permitted if it occurs within the type in whichMis declared.
- Otherwise, the type or member is inaccessible, and a compile-time error occurs.
end note]
[Example: In the following code
public class A { public static int X; internal static int Y; private static int Z; } internal class B { public static int X; internal static int Y; private static int Z; public class C { public static int X; internal static int Y; private static int Z; } private class D { public static int X; internal static int Y; private static int Z; } }
the classes and members have the following accessibility domains:
- The accessibility domain of
AandA.Xis unlimited.
- The accessibility domain of A.Y, B, B.X, B.Y, B.C, B.C.X, and B.C.Y is the program text of the containing program.
- The accessibility domain of
A.Zis the program text ofA.
- The accessibility domain of
B.ZandB.Dis the program text ofB, including the program text ofB.CandB.D.
- The accessibility domain of
B.C.Zis the program text ofB.C.
- The accessibility domain of
B.D.XandB.D.Yis the program text ofB, including the program text ofB.CandB.D.
- The accessibility domain of
B.D.Zis the program text ofB.D.
As the example illustrates, the accessibility domain of a member is never larger than that of a containing
type. For example, even though all X members have public declared accessibility, all but A.X have
accessibility domains that are constrained by a containing type. end example]
As described in §10.4, all members of a base class, except for instance constructors, finalizers, and static constructors are inherited by derived types. This includes even private members of a base class. However, the accessibility domain of a private member includes only the program text of the type in which the member is declared. [Example: In the following code
class A { int x; static void F(B b) { b.x = 1; // Ok } } class B: A { static void F(B b) { b.x = 1; // Error, x not accessible } }
the B class inherits the private member x from the A class. Because the member is private, it is only
accessible within the class-body of A. Thus, the access to b.x succeeds in the A.F method, but fails in the
B.F method. end example]