Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C-sharp language specification.2004.pdf
Скачиваний:
15
Добавлен:
23.08.2013
Размер:
3 Мб
Скачать
☆

C# LANGUAGE SPECIFICATION

1A class declaration can contain declarations of constants, fields, methods, properties, events, indexers,

2operators, instance constructors, destructors, static constructors, and types.

3The members of object and string correspond directly to the members of the class types they alias:

4• The members of object are the members of the System.Object class.

5• The members of string are the members of the System.String class.

610.4.5 Interface members

7The members of an interface are the members declared in the interface and in all base interfaces of the

8interface. [Note: The members in class object are not, strictly speaking, members of any interface (§20.2).

9However, the members in class object are available via member lookup in any interface type (§14.3). end

10note]

1110.4.6 Array members

12The members of an array are the members inherited from class System.Array.

1310.4.7 Delegate members

14The members of a delegate are the members inherited from class System.Delegate.

1510.5 Member access

16Declarations of members allow control over member access. The accessibility of a member is established by

17the declared accessibility (§10.5.1) of the member combined with the accessibility of the immediately

18containing type, if any.

19When access to a particular member is allowed, the member is said to be accessible. Conversely, when

20access to a particular member is disallowed, the member is said to be inaccessible. Access to a member is

21permitted when the textual location in which the access takes place is included in the accessibility domain

22(§10.5.2) of the member.

2310.5.1 Declared accessibility

24The declared accessibility of a member can be one of the following:

25• Public, which is selected by including a public modifier in the member declaration. The intuitive

26meaning of public is “access not limited”.

27• Protected, which is selected by including a protected modifier in the member declaration. The

28intuitive meaning of protected is “access limited to the containing class or types derived from the

29containing class”.

30• Internal, which is selected by including an internal modifier in the member declaration. The intuitive

31meaning of internal is “access limited to this program”.

32• Protected internal, which is selected by including both a protected and an internal modifier in the

33member declaration. The intuitive meaning of protected internal is “access limited to this program

34or types derived from the containing class”.

35• Private, which is selected by including a private modifier in the member declaration. The intuitive

36meaning of private is “access limited to the containing type”.

37Depending on the context in which a member declaration takes place, only certain types of declared

38accessibility are permitted. Furthermore, when a member declaration does not include any access modifiers,

39the context in which the declaration takes place determines the default declared accessibility.

40• Namespaces implicitly have public declared accessibility. No access modifiers are allowed on

41namespace declarations.

88

Chapter 10 Basic concepts

1• Types declared in compilation units or namespaces can have public or internal declared

2accessibility and default to internal declared accessibility.

3• Class members can have any of the five kinds of declared accessibility and default to private declared

4accessibility. [Note: A type declared as a member of a class can have any of the five kinds of declared

5accessibility, whereas a type declared as a member of a namespace can have only public or internal

6declared accessibility. end note]

7• Struct members can have public, internal, or private declared accessibility and default to

8private declared accessibility because structs are implicitly sealed. Struct members introduced in a

9struct (that is, not inherited by that struct) cannot have protected or protected internal declared

10accessibility. [Note: A type declared as a member of a struct can have public, internal, or private

11declared accessibility, whereas a type declared as a member of a namespace can have only public or

12internal declared accessibility. end note]

13• Interface members implicitly have public declared accessibility. No access modifiers are allowed on

14interface member declarations.

15• Enumeration members implicitly have public declared accessibility. No access modifiers are allowed

16on enumeration member declarations.

1710.5.2 Accessibility domains

18The accessibility domain of a member consists of the (possibly disjoint) sections of program text in which

19access to the member is permitted. For purposes of defining the accessibility domain of a member, a member

20is 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

21within another type. Furthermore, the text of a program is defined as all source text contained in all source

22files of that program, and the source text of a type is defined as all source text contained between the

23opening and closing “{” and “}” tokens in the class-body, struct-body, interface-body, or enum-body of all

24declarations for the type (including, possibly, multiple partial declarations and all types that are nested

25within the type).

26The accessibility domain of a predefined type (such as object, int, or double) is unlimited.

27The accessibility domain of a top-level type T that is declared in a program P is defined as follows:

28• If the declared accessibility of T is public, the accessibility domain of T is the program text of P and

29any program that references P.

30• If the declared accessibility of T is internal, the accessibility domain of T is the program text of P.

31[Note: From these definitions it follows that the accessibility domain of a top-level type is always at least the

32program text of the program in which that type is declared. end note]

33The accessibility domain of a nested member M declared in a type T within a program P, is defined as

34follows (noting that M itself might possibly be a type):

35• If the declared accessibility of M is public, the accessibility domain of M is the accessibility domain

36of T.

37• If the declared accessibility of M is protected internal, let D be the union of the program text of P

38and the program text of any type derived from T, which is declared outside P. The accessibility domain

39of M is the intersection of the accessibility domain of T with D.

40• If the declared accessibility of M is protected, let D be the union of the program text of T and the

41program text of any type derived from T. The accessibility domain of M is the intersection of the

42accessibility domain of T with D.

43• If the declared accessibility of M is internal, the accessibility domain of M is the intersection of the

44accessibility domain of T with the program text of P.

45• If the declared accessibility of M is private, the accessibility domain of M is the program text of T.

89

C# LANGUAGE SPECIFICATION

1[Note: From these definitions it follows that the accessibility domain of a nested member is always at least

2the program text of the type in which the member is declared. Furthermore, it follows that the accessibility

3domain of a member is never more inclusive than the accessibility domain of the type in which the member

4is declared. end note]

5[Note: In intuitive terms, when a type or member M is accessed, the following steps are evaluated to ensure

6that the access is permitted:

7• First, if M is declared within a type (as opposed to a compilation unit or a namespace), a compile-time

8error occurs if that type is not accessible.

9• Then, if M is public, the access is permitted.

10• Otherwise, if M is protected internal, the access is permitted if it occurs within the program in

11which M is declared, or if it occurs within a class derived from the class in which M is declared and takes

12place through the derived class type (§10.5.3).

13• Otherwise, if M is protected, the access is permitted if it occurs within the class in which M is declared,

14or if it occurs within a class derived from the class in which M is declared and takes place through the

15derived class type (§10.5.3).

16• Otherwise, if M is internal, the access is permitted if it occurs within the program in which M is

17declared.

18• Otherwise, if M is private, the access is permitted if it occurs within the type in which M is declared.

19• Otherwise, the type or member is inaccessible, and a compile-time error occurs.

20end note]

21[Example: In the following code

22public class A

23{

24

public static int X;

25

internal static int Y;

26private static int Z;

27}

28internal class B

29{

30

public static int X;

31

internal static int Y;

32

private static int Z;

33

public class C

34

{

35

public static int X;

36

internal static int Y;

37

private static int Z;

38

}

39

private class D

40

{

41

public static int X;

42

internal static int Y;

43

private static int Z;

44}

45}

46the classes and members have the following accessibility domains:

47• The accessibility domain of A and A.X is unlimited.

48• 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

49containing program.

50• The accessibility domain of A.Z is the program text of A.

90

Chapter 10 Basic concepts

1• The accessibility domain of B.Z and B.D is the program text of B, including the program text of B.C

2and B.D.

3• The accessibility domain of B.C.Z is the program text of B.C.

4• The accessibility domain of B.D.X and B.D.Y is the program text of B, including the program text of

5B.C and B.D.

6• The accessibility domain of B.D.Z is the program text of B.D.

7As the example illustrates, the accessibility domain of a member is never larger than that of a containing

8type. For example, even though all X members have public declared accessibility, all but A.X have

9accessibility domains that are constrained by a containing type. end example]

10As described in §10.4, all members of a base class, except for instance constructors, destructors, and

11static constructors are inherited by derived types. This includes even private members of a base class.

12However, the accessibility domain of a private member includes only the program text of the type in

13which the member is declared. [Example: In the following code

14class A

15{

16

int x;

 

17

static void F(B b) {

18

b.x = 1;

// Ok

19}

20}

21class B: A

22{

23

static

void

F(B b) {

24

b.x

= 1;

// Error, x not accessible

25}

26}

27the B class inherits the private member x from the A class. Because the member is private, it is only

28accessible within the class-body of A. Thus, the access to b.x succeeds in the A.F method, but fails in the

29B.F method. end example]

3010.5.3 Protected access for instance members

31When a protected instance member is accessed outside the program text of the class in which it is

32declared, and when a protected internal instance member is accessed outside the program text of the

33program in which it is declared, the access is required to take place through an instance of the derived class

34type in which the access occurs. Let B be a base class that declares a protected instance member M, and let D

35be a class that derives from B. Within the class-body of D, access to M can take one of the following forms:

36• An unqualified type-name or primary-expression of the form M.

37• A primary-expression of the form E.M, provided the type of E is D or a class derived from D.

38• A primary-expression of the form base.M.

39In addition to these forms of access, a derived class can access a protected instance constructor of a base

40class in a constructor-initializer (§17.10.1).

41[Example: In the following code

42public class A

43{

44

protected int x;

45

static void F(A a, B b) {

46

a.x = 1;

// Ok

47

b.x = 1;

// Ok

48}

49}

91

Соседние файлы в предмете Электротехника