Jo Shields a575963da9 Imported Upstream version 3.6.0
Former-commit-id: da6be194a6b1221998fc28233f2503bd61dd9d14
2014-08-13 10:39:27 +01:00

15 lines
3.9 KiB
XML

<?xml version="1.0"?>
<clause number="12.1.7" title="Local variables">
<paragraph>A local variable is declared by a <non_terminal where="15.5.1">local-variable-declaration</non_terminal>, which may occur in a block, a <non_terminal where="15.8.3">for-statement</non_terminal>, a <non_terminal where="15.7.2">switch-statement</non_terminal>, or a <non_terminal where="15.13">using-statement</non_terminal>. </paragraph>
<paragraph>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, <non_terminal where="15.8.3">for-statement</non_terminal>, <non_terminal where="15.7.2">switch-statement</non_terminal>, or <non_terminal where="15.13">using-statement</non_terminal> with which it is associated, until execution of that block, <non_terminal where="15.8.3">for-statement</non_terminal>, <non_terminal where="15.7.2">switch-statement</non_terminal>, or <non_terminal where="15.13">using-statement</non_terminal> ends in any way. (Entering an enclosed block or calling a method suspends, but does not end, execution of the current block, <non_terminal where="15.8.3">for-statement</non_terminal>, <non_terminal where="15.7.2">switch-statement</non_terminal>, or <non_terminal where="15.13">using-statement</non_terminal>.) If the parent block, <non_terminal where="15.8.3">for-statement</non_terminal>, <non_terminal where="15.7.2">switch-statement</non_terminal>, or <non_terminal where="15.13">using-statement</non_terminal> is entered recursively, a new instance of the local variable is created each time, and its <non_terminal where="15.5.1">local-variable-initializer</non_terminal>, if any, is evaluated each time. </paragraph>
<paragraph>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 <non_terminal where="15.5.1">local-variable-declaration</non_terminal> may include a <non_terminal where="15.5.1">local-variable-initializer</non_terminal>, in which case the variable is considered definitely assigned in its entire scope, except within the expression provided in the <non_terminal where="15.5.1">local-variable-initializer</non_terminal>. </paragraph>
<paragraph>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 <non_terminal where="15.5.1">local-variable-declarator</non_terminal>. </paragraph>
<paragraph>
<note>[Note: 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. </note>
</paragraph>
<paragraph>
<note>The storage referred to by a local reference variable is reclaimed independently of the lifetime of that local reference variable (<hyperlink>10.9</hyperlink>). end note]</note>
</paragraph>
<paragraph>A local variable is also declared by a <non_terminal where="15.8.4">foreach-statement</non_terminal> and by a <non_terminal where="15.10">specific-catch-clause</non_terminal> for a <non_terminal where="15.10">try-statement</non_terminal>. For a <non_terminal where="15.8.4">foreach-statement</non_terminal>, the local variable is an iteration variable (<hyperlink>15.8.4</hyperlink>). For a <non_terminal where="15.10">specific-catch-clause</non_terminal>, the local variable is an exception variable (<hyperlink>15.10</hyperlink>). A local variable declared by a <non_terminal where="15.8.4">foreach-statement</non_terminal> or <non_terminal where="15.10">specific-catch-clause</non_terminal> is considered definitely assigned in its entire scope. </paragraph>
</clause>