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

C# LANGUAGE SPECIFICATION

1class Text

2{

3

public Text(): this(0, 0, null) {}

4

public Text(int x, int y): this(x, y, null) {}

5

public Text(int x, int y, string s) {

6

// Actual constructor implementation

7}

8}

9the first two instance constructors merely provide the default values for the missing arguments. Both use a

10this(…) constructor initializer to invoke the third instance constructor, which actually does the work of

11initializing the new instance. The effect is that of optional constructor parameters:

12

Text

t1

=

new

Text();

//

Same

as

Text(0,

0, null)

13

Text

t2

=

new

Text(5, 10);

//

Same

as

Text(5,

10, null)

14Text t3 = new Text(5, 20, "Hello");

15end note]

1617.11 Static constructors

17A static constructor is a member that implements the actions required to initialize a class. Static

18constructors are declared using static-constructor-declarations:

19static-constructor-declaration:

20

attributesopt static-constructor-modifiers identifier ( ) static-constructor-body

21

static-constructor-modifiers:

22

externopt static

23static externopt

24static-constructor-body:

25

block

26;

27A static-constructor-declaration can include a set of attributes (§24) and an extern modifier (§17.5.7).

28The identifier of a static-constructor-declaration shall name the class in which the static constructor is

29declared. If any other name is specified, a compile-time error occurs.

30When a static constructor declaration includes an extern modifier, the static constructor is said to be an

31external static constructor. Because an external static constructor declaration provides no actual

32implementation, its static-constructor-body consists of a semicolon. For all other static constructor

33declarations, the static-constructor-body consists of a block, which specifies the statements to execute in

34order to initialize the class. This corresponds exactly to the method-body of a static method with a void

35return type (§17.5.8).

36Static constructors are not inherited, and cannot be called directly.

37The static constructor for a non-generic class executes at most once in a given application domain. The static

38constructor for a generic class declaration executes at most once for each closed constructed type

39constructed from the class declaration (§26.1.5). The execution of a static constructor is triggered by the first

40of the following events to occur within an application domain:

41An instance of the class is created.

42Any of the static members of the class are referenced.

43If a class contains the Main method (§10.1) in which execution begins, the static constructor for that class

44executes before the Main method is called. If a class contains any static fields with initializers, those

45initializers are executed in textual order immediately prior to executing the static constructor (§17.4.5).

46[Example: The example

47using System;

316

Chapter 17 Classes

1class Test

2{

3

static void Main() {

4

A.F();

5

B.F();

6}

7}

8class A

9{

10

static A() {

11

Console.WriteLine("Init A");

12

}

13

public static void F() {

14

Console.WriteLine("A.F");

15}

16}

17class B

18{

19

static B() {

20

Console.WriteLine("Init B");

21

}

22

public static void F() {

23

Console.WriteLine("B.F");

24}

25}

26shall produce the output:

27Init A

28A.F

29Init B

30B.F

31because the execution of A's static constructor is triggered by the call to A.F, and the execution of B's static

32constructor is triggered by the call to B.F. end example]

33It is possible to construct circular dependencies that allow static fields with variable initializers to be

34observed in their default value state.

35[Example: The example

36using System;

37class A

38{

39

public static int X;

40static A() { X = B.Y + 1;}

41}

42class B

43{

44

public static int Y = A.X + 1;

45

static B() {}

46

static void Main() {

47

Console.WriteLine("X = {0}, Y = {1}", A.X, B.Y);

48}

49}

50produces the output

51X = 1, Y = 2

52To execute the Main method, the system first runs the initializer for B.Y, prior to class B's static constructor.

53Y's initializer causes A's static constructor to be run because the value of A.X is referenced. The static

54constructor of A in turn proceeds to compute the value of X, and in doing so fetches the default value of Y,

55which is zero. A.X is thus initialized to 1. The process of running A's static field initializers and static

56constructor then completes, returning to the calculation of the initial value of Y, the result of which

57becomes 2. end example]

317

C# LANGUAGE SPECIFICATION

117.12 Destructors

2A destructor is a member that implements the actions required to destruct an instance of a class. A

3destructor is declared using a destructor-declaration:

4destructor-declaration:

5

attributesopt externopt ~ identifier ( ) destructor-body

6

destructor-body:

7

block

8;

9A destructor-declaration can include a set of attributes (§24).

10The identifier of a destructor-declarator shall name the class in which the destructor is declared. If any other

11name is specified, a compile-time error occurs.

12When a destructor declaration includes an extern modifier, the destructor is said to be an external

13destructor. Because an external destructor declaration provides no actual implementation, its destructor-

14body consists of a semicolon. For all other destructors, the destructor-body consists of a block, which

15specifies the statements to execute in order to destruct an instance of the class. A destructor-body

16corresponds exactly to the method-body of an instance method with a void return type (§17.5.8).

17Destructors are not inherited. Thus, a class has no destructors other than the one which can be declared in

18that class.

19[Note: Since a destructor is required to have no parameters, it cannot be overloaded, so a class can have, at

20most, one destructor. end note]

21Destructors are invoked automatically, and cannot be invoked explicitly. An instance becomes eligible for

22destruction when it is no longer possible for any code to use that instance. Execution of the destructor for the

23instance can occur at any time after the instance becomes eligible for destruction. When an instance is

24destructed, the destructors in that instance’s inheritance chain are called, in order, from most derived to least

25derived [Example: The output of the example

26using System;

27class A

28{

29

~A() {

30

Console.WriteLine("A's destructor");

31}

32}

33class B: A

34{

35

~B() {

36

Console.WriteLine("B's destructor");

37}

38}

39class Test

40{

41

static void Main() {

42

B b = new B();

43

b = null;

44

GC.Collect();

45

GC.WaitForPendingFinalizers();

46}

47}

48is

49B’s destructor

50A’s destructor

51since destructors in an inheritance chain are called in order, from most derived to least derived. The

52GC.Collect method is not required, but can be provided by an implementation. end example]

318

Chapter 17 Classes

1Destructors can be implemented by overriding the virtual method Finalize on System.Object. In any

2event, C# programs are not permitted to override this method or call it (or overrides of it) directly.

3[Example: For instance, the program

4class A

5{

6

protected override void Finalize() {} // error

7

public void F() {

 

8

this.Finalize();

// error

9}

10}

11contains two errors. end example]

12The compiler behaves as if System.Object.Finalize, and overrides of it, does not exist at all.

13[Example: Thus, this program:

14class A

15{

16

void Finalize() {}

// permitted

17}

18is valid and the method shown hides System.Object's Finalize method, without requiring the new

19modifier. end example]

20For a discussion of the behavior when an exception is thrown from a destructor, see §23.3.

319

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