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

Chapter 17 Classes

1from the field result. Declaring finished as a volatile field prevents any such inconsistency. end

2example]

317.4.4 Field initialization

4The initial value of a field, whether it be a static field or an instance field, is the default value (§12.2) of the

5field’s type. It is not possible to observe the value of a field before this default initialization has occurred,

6and a field is thus never “uninitialized”. [Example: The example

7using System;

8class Test

9{

10

static bool b;

11

int i;

12

static void Main() {

13

Test t = new Test();

14

Console.WriteLine("b = {0}, i = {1}", b, t.i);

15}

16}

17produces the output

18b = False, i = 0

19because b and i are both automatically initialized to default values. end example]

2017.4.5 Variable initializers

21Field declarations can include variable-initializers. For static fields, variable initializers correspond to

22assignment statements that are executed during class initialization. For instance fields, variable initializers

23correspond to assignment statements that are executed when an instance of the class is created.

24[Example: The example

25using System;

26class Test

27{

28

static double x = Math.Sqrt(2.0);

29

int i = 100;

30

string s = "Hello";

31

static void Main() {

32

Test a = new Test();

33

Console.WriteLine("x = {0}, i = {1}, s = {2}", x, a.i, a.s);

34}

35}

36produces the output

37x = 1.4142135623731, i = 100, s = Hello

38because an assignment to x occurs when static field initializers execute and assignments to i and s occur

39when the instance field initializers execute. end example]

40The default value initialization described in §17.4.3 occurs for all fields, including fields that have variable

41initializers. Thus, when a class is initialized, all static fields in that class are first initialized to their default

42values, and then the static field initializers are executed in textual order. Likewise, when an instance of a

43class is created, all instance fields in that instance are first initialized to their default values, and then the

44instance field initializers are executed in textual order. When there are field declarations in multiple partial

45type declarations for the same type, the order of the parts is unspecified. However, within each part the field

46initializers are executed in order.

47It is possible for static fields with variable initializers to be observed in their default value state. [Example:

48However, this is strongly discouraged as a matter of style. The example

49using System;

275

C# LANGUAGE SPECIFICATION

1class Test

2{

3

static int a = b + 1;

4

static int b = a + 1;

5

static void Main() {

6

Console.WriteLine("a = {0}, b = {1}", a, b);

7}

8}

9exhibits this behavior. Despite the circular definitions of a and b, the program is valid. It results in the output

10a = 1, b = 2

11because the static fields a and b are initialized to 0 (the default value for int) before their initializers are

12executed. When the initializer for a runs, the value of b is zero, and so a is initialized to 1. When the

13initializer for b runs, the value of a is already 1, and so b is initialized to 2. end example]

1417.4.5.1 Static field initialization

15The static field variable initializers of a class declaration correspond to a sequence of assignments that are

16executed in the textual order in which they appear in the class declaration. If a static constructor (§17.11)

17exists in the class, execution of the static field initializers occurs immediately prior to executing that static

18constructor. Otherwise, the static field initializers are executed at an implementation-dependent time prior to

19the first use of a static field of that class. [Example: The example

20using System;

21class Test

22{

23

static void Main() {

24

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

25

}

26

public static int f(string s) {

27

Console.WriteLine(s);

28

return 1;

29}

30}

31class A

32{

33public static int X = Test.f("Init A");

34}

35class B

36{

37public static int Y = Test.f("Init B");

38}

39might produce either the output:

40Init A

41Init B

421 1

43or the output:

44Init B

45Init A

461 1

47because the execution of X's initializer and Y's initializer could occur in either order; they are only

48constrained to occur before the references to those fields. However, in the example:

49using System;

276

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