The accessibility domain of a member is 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 program text of a project is defined as all program text contained in all source files of the project, and the program text of a type is defined as all program text contained between the opening and closing "{
" and "}
" tokens in the class-body, struct-body, interface-body, or enum-body of the type (including, possibly, 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
declared in a project P
is defined as follows:
T
is public
, the accessibility domain of T
is the program text of P
and any project that references P
.T
is internal
, the accessibility domain of T
is the program text of P
.From these definitions it follows that the accessibility domain of a top-level type is always at least the program text of the project in which the type is declared.
The accessibility domain of a nested member M
declared in a type T
within a project P
is defined as follows (noting that M may itself possibly be a type):
M
is public
, the accessibility domain of M
is the accessibility domain of T
.M
is protected
internal
, the accessibility domain of M
is the intersection of the accessibility domain of T
with the program text of P
and the program text of any type derived from T
declared outside P
.M
is protected
, the accessibility domain of M
is the intersection of the accessibility domain of T
with the program text of T
and any type derived from T
.M
is internal
, the accessibility domain of M
is the intersection of the accessibility domain of T
with the program text of P
.M
is private
, the accessibility domain of M
is the program text of T
.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.
In intuitive terms, when a type or member M
is accessed, the following steps are evaluated to ensure that the access is permitted:
M
is declared within a type (as opposed to a compilation unit or a namespace), an error occurs if that type is not accessible.M
is public
, the access is permitted.M
is protected
internal
, the access is permitted if it occurs within the project in which M
is declared, or if it occurs within a class derived from the class in which M
is declared and takes place through the derived class type (§3.3.3).M
is protected
, the access is permitted if it occurs within the class in which M
is declared, or if it occurs within a class derived from the class in which M
is declared and takes place through the derived class type (§3.3.3).M
is internal
, the access is permitted if it occurs within the project in which M
is declared.M
is private
, the access is permitted if it occurs within the type in which M
is declared.In the example
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:
A
and A.X
is unlimited.A.Y
, B
, B.X
, B.Y
, B.C
, B.C.X
, and B.C.Y
is the program text of the containing project.A.Z
is the program text of A
.B.Z
and B.D
is the program text of B
, including the program text of B.C
and B.D
.B.C.Z
is the program text of B.C
.B.D.X
, B.D.Y
, and B.D.Z
is the program text of B.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.
As described in §3.2, all members of a base class, except for constructors and destructors, 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. In the example
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.