X hits on this document





98 / 396

Chapter ‎18   Unsafe code

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

Following the normal completion of a function member invocation, each variable that was passed as an output parameter is considered assigned in that execution path.

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

Every output parameter of a function member must be definitely assigned (§‎5.3) before the function member returns normally.

Within an instance constructor of a struct type, the this keyword behaves exactly as an output parameter of the struct type (§‎7.5.7).

5.1.7 Local variables

A local variable is declared by a local-variable-declaration, which may occur in a block, a for-statement, a switch-statement, or a using-statement.

The lifetime of a local variable is the portion of program execution during which storage is guaranteed to be reserved for it. This lifetime extends from entry into the block, for-statement, switch-statement, or using-statement with which it is associated, until execution of that block, for-statement, switch-statement, or using-statement ends in any way. (Entering an enclosed block or calling a method suspends, but does not end, execution of the current block, for-statement, switch-statement, or using-statement.) If the parent block, for-statement, switch-statement, or using-statement is entered recursively, a new instance of the local variable is created each time, and its local-variable-initializer, if any, is evaluated each time.

A local variable is not automatically initialized and thus has no default value. For the purpose of definite assignment checking, a local variable is considered initially unassigned. A local-variable-declaration may include a local-variable-initializer, in which case the variable is considered definitely assigned in its entire scope, except within the expression provided in the local-variable-initializer.

Within the scope of a local variable, it is a compile-time error to refer to that local variable in a textual position that precedes its local-variable-declarator.

The actual lifetime of a local variable is implementation-dependent. For example, a compiler might statically determine that a local variable in a block is only used for a small portion of that block. Using this analysis, the compiler could generate code that results in the variable’s storage having a shorter lifetime than its containing block.

The storage referred to by a local reference variable is reclaimed independently of the lifetime of that local reference variable (§‎3.9).

A local variable is also declared by a foreach-statement and by a specific-catch-clause for a try-statement. For a foreach-statement, the local variable is an iteration variable (§‎8.8.4). For a specific-catch-clause, the local variable is an exception variable (§‎8.10). A local variable declared by a foreach-statement or specific-catch-clause is considered definitely assigned in its entire scope.

5.2 Default values

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

Static variables.

Instance variables of class instances.

Array elements.

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

Copyright Microsoft Corporation 1999-2003. All Rights Reserved.85

Document info
Document views588
Page views588
Page last viewedTue Oct 25 19:07:19 UTC 2016