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

Chapter 12 Variables

1A variable need not be definitely assigned before it can be passed as an output parameter in a function

2member invocation.

3Following the normal completion of a function member invocation, each variable that was passed as an

4output parameter is considered assigned in that execution path.

5Within a function member, an output parameter is considered initially unassigned.

6Every output parameter of a function member shall be definitely assigned (§12.3) before the function

7member returns normally.

8Within an instance constructor of a struct type, the this keyword behaves exactly as an output or reference

9parameter of the struct type, depending on whether the constructor declaration includes a constructor

10initializer (§14.5.7).

1112.1.7 Local variables

12A local variable is declared by a local-variable-declaration, foreach-statement, or specific-catch-clause of a

13try-statement. For a foreach-statement, the local variable is an iteration variable (§15.8.4). For a specific-

14catch-clause, the local variable is an exception variable (§15.10). A local variable declared by a foreach-

15statement or specific-catch-clause is considered initially assigned.

16A local-variable-declaration can occur in a block, a for-statement, a switch-block, or a using-statement.

17The lifetime of a local variable is the portion of program execution during which storage is guaranteed to be

18reserved for it. This lifetime extends from entry into the scope with which it is associated, at least until

19execution of that scope ends in some way. (Entering an enclosed block, calling a method, or yielding a value

20from an iterator block suspends, but does not end, execution of the current scope.) If the local variable is

21captured by an anonymous method, the lifetime of the variable is extended at least until all referencing

22delegates are eligible for garbage collection (§14.5.14.3.1). If the parent scope is entered recursively or

23iteratively, a new instance of the local variable is created each time, and its local-variable-initializer, if any,

24is evaluated each time. [Note: A local variable is instantiated each time its scope is entered. This behavior is

25visible to user code containing anonymous methods. end note]

26A local variable introduced by a local-variable-declaration is not automatically initialized and thus has no

27default value. Such a local variable is considered initially unassigned. A local-variable-declaration can

28include a local-variable-initializer, in which case the variable is considered definitely assigned in its entire

29scope, except within the expression provided in the local-variable-initializer.

30Within the scope of a local variable, it is a compile-time error to refer to that local variable in a textual

31position that precedes its local-variable-declarator.

32[Note: The actual lifetime of a local variable is implementation-dependent. For example, a compiler might

33statically determine that a local variable in a block is only used for a small portion of that block. Using this

34analysis, the compiler could generate code that results in the variable’s storage having a shorter lifetime than

35its containing block.

36The storage referred to by a local reference variable is reclaimed independently of the lifetime of that local

37reference variable (§10.9). end note]

3812.2 Default values

39The following categories of variables are automatically initialized to their default values:

40Static variables.

41Instance variables of class instances.

42Array elements.

43The default value of a variable depends on the type of the variable and is determined as follows:

117

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