13.07.2015 Views

C# Language Specification - Willy .Net

C# Language Specification - Willy .Net

C# Language Specification - Willy .Net

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 12 Variables• Following the normal completion of a function member invocation, each variable that was passed as anoutput 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 (§12.3) before the functionmember returns normally.Within an instance constructor of a struct type, the this keyword behaves exactly as an output parameter ofthe struct type (§14.5.7).12.1.7 Local variablesA local variable is declared by a local-variable-declaration, which may occur in a block, a for-statement, aswitch-statement, or a using-statement.The lifetime of a local variable is the portion of program execution during which storage is guaranteed to bereserved for it. This lifetime extends from entry into the block, for-statement, switch-statement, or usingstatementwith which it is associated, until execution of that block, for-statement, switch-statement, or usingstatementends 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, forstatement,switch-statement, or using-statement is entered recursively, a new instance of the local variable iscreated 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 definiteassignment checking, a local variable is considered initially unassigned. A local-variable-declaration mayinclude a local-variable-initializer, in which case the variable is considered definitely assigned in its entirescope, 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 textualposition that precedes its local-variable-declarator.[Note: The actual lifetime of a local variable is implementation-dependent. For example, a compiler mightstatically determine that a local variable in a block is only used for a small portion of that block. Using thisanalysis, the compiler could generate code that results in the variable’s storage having a shorter lifetime thanits containing block.The storage referred to by a local reference variable is reclaimed independently of the lifetime of that localreference variable (§10.9). end note]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 (§15.8.4). For a specific-catch-clause, thelocal variable is an exception variable (§15.10). A local variable declared by a foreach-statement or specificcatch-clauseis considered definitely assigned in its entire scope.12.2 Default valuesThe 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:• For a variable of a value-type, the default value is the same as the value computed by the value-type’sdefault constructor (§11.1.1).• For a variable of a reference-type, the default value is null.101

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!