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

Chapter 12 Variables

1The definite assignment state of v before expr-first is the same as the definite assignment state of v

2before expr.

3The definite assignment state of v before expr-second is definitely assigned if the state of v after expr-

4first is either definitely assigned or “definitely assigned after false expression”. Otherwise, it is not

5definitely assigned.

6The definite assignment statement of v after expr is determined by:

7o If the state of v after expr-first is definitely assigned, then the state of v after expr is definitely

8assigned.

9o Otherwise, if the state of v after expr-second is definitely assigned, and the state of v after expr-first

10is “definitely assigned after true expression”, then the state of v after expr is definitely assigned.

11o Otherwise, if the state of v after expr-second is definitely assigned or “definitely assigned after false

12expression”, then the state of v after expr is “definitely assigned after false expression”.

13o Otherwise, if the state of v after expr-first is “definitely assigned after true expression”, and the state

14of v after expr-second is “definitely assigned after true expression”, then the state of v after expr is

15“definitely assigned after true expression”.

16o Otherwise, the state of v after expr is not definitely assigned.

17[Example: In the following code

18class A

19{

20

static void G(int x, int y) {

21

int i;

22

if (x >= 0 || (i = y) >= 0) {

23

// i not definitely assigned

24

}

25

else {

26

// i definitely assigned

27

}

28

// i not definitely assigned

29}

30}

31the variable i is considered definitely assigned in one of the embedded statements of an if statement but not

32in the other. In the if statement in method G, the variable i is definitely assigned in the second embedded

33statement because execution of the expression (i = y) always precedes execution of this embedded

34statement. In contrast, the variable i is not definitely assigned in the first embedded statement, since

35x >= 0 might have tested true, resulting in the variable i's being unassigned. end example]

3612.3.3.25 ! expressions

37For an expression expr of the form ! expr-operand:

38The definite assignment state of v before expr-operand is the same as the definite assignment state of v

39before expr.

40The definite assignment state of v after expr is determined by:

41o If the state of v after expr-operand is definitely assigned, then the state of v after expr is definitely

42assigned.

43o If the state of v after expr-operand is not definitely assigned, then the state of v after expr is not

44definitely assigned.

45o If the state of v after expr-operand is “definitely assigned after false expression”, then the state of v

46after expr is “definitely assigned after true expression”.

47o If the state of v after expr-operand is “definitely assigned after true expression”, then the state of v

48after expr is “definitely assigned after false expression”.

127

C# LANGUAGE SPECIFICATION

112.3.3.26 ?: expressions

2For an expression expr of the form expr-cond ? expr-true : expr-false:

3The definite assignment state of v before expr-cond is the same as the state of v before expr.

4The definite assignment state of v before expr-true is definitely assigned if and only if the state of v after

5expr-cond is definitely assigned or “definitely assigned after true expression”.

6The definite assignment state of v before expr-false is definitely assigned if and only if the state of v

7after expr-cond is definitely assigned or “definitely assigned after false expression”.

8The definite assignment state of v after expr is determined by:

9o If expr-cond is a constant expression (§14.15) with value true then the state of v after expr is the

10same as the state of v after expr-true.

11o Otherwise, if expr-cond is a constant expression (§14.15) with value false then the state of v after

12expr is the same as the state of v after expr-false.

13o Otherwise, if the state of v after expr-true is definitely assigned and the state of v after expr-false is

14definitely assigned, then the state of v after expr is definitely assigned.

15o Otherwise, the state of v after expr is not definitely assigned.

1612.3.3.27 Anonymous method expressions

17The definite assignment state of a parameter of an anonymous method (§14.5.14) is the same as for a

18parameter of a named method. That is, reference parameters and value parameters are initially definitely

19assigned and output parameters are initially unassigned. Furthermore, output parameters shall be definitely

20assigned before the anonymous method returns normally (§12.1.6).

21The definite assignment state of an outer variable v on the control transfer to the block of an anonymous-

22method-expression is the same as the definite assignment state of v before the anonymous-method-

23expression. That is, definite assignment of outer variables is inherited from the context of the anonymous-

24method-expression. Within the block of an anonymous-method-expression, definite assignment evolves as in

25a normal block (§12.3.3).

26The definite assignment state of a variable v after an anonymous-method-expression is the same as its

27definite assignment state before the anonymous-method-expression.

28[Example: The example

29delegate bool Filter(int i);

30void F() {

31

int max;

32

// Error, max is not definitely assigned

33

Filter f = delegate(int n) { return n < max; }

34

max = 5;

35DoWork(f);

36}

37generates a compile-time error since max is not definitely assigned where the anonymous method is

38declared. end example] [Example: The example

39delegate void D();

40void F() {

41

int n;

42

D d = delegate { n = 1; };

43

d();

44

// Error, n is not definitely assigned

45Console.WriteLine(n);

46}

128

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