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

Chapter 17 Classes

1When an instance member is referenced in a member-access (§14.5.4) of the form E.M, E shall denote an

2instance of a type that has a member M. It is a compile-time error for E to denote a type.

3Every instance of a class contains a separate set of all instance fields of the class.

4An instance function member (method, property, indexer, event, instance constructor, or destructor)

5operates on a given instance of the class, and this instance can be accessed as this (§14.5.7).

6[Example: The following example illustrates the rules for accessing static and instance members:

7class Test

8{

9

int x;

 

10

static int y;

 

11

void F() {

 

12

x = 1;

// Ok, same as this.x = 1

13

y = 1;

// Ok, same as Test.y = 1

14

}

 

15

static void G() {

16

x = 1;

// Error, cannot access this.x

17

y = 1;

// Ok, same as Test.y = 1

18

}

 

19

static void Main() {

20

Test t = new Test();

21

t.x = 1;

// Ok

22

t.y = 1;

// Error, cannot access static member through instance

23

Test.x = 1; // Error, cannot access instance member through type

24

Test.y = 1; // Ok

25}

26}

27The F method shows that in an instance function member, a simple-name (§14.5.2) can be used to access

28both instance members and static members. The G method shows that in a static function member, it is a

29compile-time error to access an instance member through a simple-name. The Main method shows that in a

30member-access (§14.5.4), instance members shall be accessed through instances, and static members shall

31be accessed through types. end example]

3217.2.6 Nested types

33A type declared within a class or struct is called a nested type. A type that is declared within a compilation

34unit or namespace is called a non-nested type. [Example: In the following example:

35using System;

36class A

37{

38

class B

39

{

40

static void F() {

41

Console.WriteLine("A.B.F");

42

}

43}

44}

45class B is a nested type because it is declared within class A, and class A is a non-nested type because it is

46declared within a compilation unit. end example]

4717.2.6.1 Fully qualified name

48The fully qualified name (§10.8.2) for a nested type declaration is S.N where S is the fully qualified name of

49the type declaration in which type N is declared and N is the unqualified name (§10.8.1) of the nested type

50declaration (including any generic dimensionality indication).

265

C# LANGUAGE SPECIFICATION

117.2.6.2 Declared accessibility

2Non-nested types can have public or internal declared accessibility and they have internal declared

3accessibility by default. Nested types can have these forms of declared accessibility too, plus one or more

4additional forms of declared accessibility, depending on whether the containing type is a class or struct:

5A nested type that is declared in a class can have any of five forms of declared accessibility (public,

6protected internal, protected, internal, or private) and, like other class members, defaults to private

7declared accessibility.

8A nested type that is declared in a struct can have any of three forms of declared accessibility (public,

9internal, or private) and, like other struct members, defaults to private declared accessibility.

10[Example: The example

11public class List

12{

13

// Private data structure

14

private class Node

15

{

16

public object Data;

17

public Node Next;

18

public Node(object data, Node next) {

19

this.Data = data;

20

this.Next = next;

21

}

22

}

23

private Node first = null;

24

private Node last = null;

25

// Public interface

26

public void AddToFront(object o) {…}

27

public void AddToBack(object o) {…}

28

public object RemoveFromFront() {…}

29

public object AddToFront() {…}

30public int Count { get {…} }

31}

32declares a private nested class Node. end example]

3317.2.6.3 Hiding

34A nested type can hide (§10.7.1.1) a base member. The new modifier (§17.2.2) is permitted on nested type

35declarations so that hiding can be expressed explicitly. [Example: The example

36using System;

37class Base

38{

39

public static void M() {

40

Console.WriteLine("Base.M");

41}

42}

43class Derived: Base

44{

45

new public class M

46

{

47

public static void F() {

48

Console.WriteLine("Derived.M.F");

49

}

50}

51}

52class Test

53{

54

static void Main() {

55

Derived.M.F();

56}

57}

266

Chapter 17 Classes

1shows a nested class M that hides the method M defined in Base. end example]

217.2.6.4 this access

3A nested type and its containing type do not have a special relationship with regard to this-access (§14.5.7).

4Specifically, this within a nested type cannot be used to refer to instance members of the containing type.

5In cases where a nested type needs access to the instance members of its containing type, access can be

6provided by providing the this for the instance of the containing type as a constructor argument for the

7nested type. [Example: The following example

8using System;

9class C

10{

11

int i = 123;

12

public void F() {

13

Nested n = new Nested(this);

14

n.G();

15

}

16

public class Nested

17

{

18

C this_c;

19

public Nested(C c) {

20

this_c = c;

21

}

22

public void G() {

23

Console.WriteLine(this_c.i);

24

}

25}

26}

27class Test

28{

29

static void Main() {

30

C c = new C();

31

c.F();

32}

33}

34shows this technique. An instance of C creates an instance of Nested, and passes its own this to Nested's

35constructor in order to provide subsequent access to C's instance members. end example]

3617.2.6.5 Access to private and protected members of the containing type

37A nested type has access to all of the members that are accessible to its containing type, including members

38of the containing type that have private and protected declared accessibility. [Example: The example

39using System;

40class C

41{

42

private static void F() {

43

Console.WriteLine("C.F");

44

}

45

public class Nested

46

{

47

public static void G() {

48

F();

49

}

50}

51}

52class Test

53{

54

static void Main() {

55

C.Nested.G();

56}

57}

267

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