📄 10.7.htm
字号:
int j = (j = 1); // Valid
}
void H() {
int a = 1, b = ++a; // Valid
}
}
</pre></span></span><span class="paragraph"><span class="example">In the F method above, the first assignment to i specifically does not refer to the field declared in the outer scope. Rather, it refers to the local variable and it results in a compile-time error because it textually precedes the declaration of the variable. In the G method, the use of j in the initializer for the declaration of j is valid because the use does not precede the <span class="non-terminal"><a href="15.5.1.htm#local-variable-declarator">local-variable-declarator</a></span>. In the H method, a subsequent <span class="non-terminal"><a href="15.5.1.htm#local-variable-declarator">local-variable-declarator</a></span> correctly refers to a local variable declared in an earlier <span class="non-terminal"><a href="15.5.1.htm#local-variable-declarator">local-variable-declarator</a></span> within the same <span class="non-terminal"><a href="15.5.1.htm#local-variable-declaration">local-variable-declaration</a></span>. end example]</span> </span><span class="paragraph"><span class="note">[Note: The scoping rules for local variables are designed to guarantee that the meaning of a name used in an expression context is always the same within a block. If the scope of a local variable were to extend only from its declaration to the end of the block, then in the example above, the first assignment would assign to the instance variable and the second assignment would assign to the local variable. In certain situations but not in the exampe above, this could lead to a compile-time error if the statements of the block were later to be rearranged.) </span></span><span class="paragraph"><span class="note">The meaning of a name within a block may differ based on the context in which the name is used. In the example <pre class="code-example">
using System;
class A {}
class Test
{
static void Main() {
string A = "hello, world";
string s = A; // expression context
Type t = typeof(A); // type context
Console.WriteLine(s); // writes "hello, world"
Console.WriteLine(t.ToString()); // writes "Type: A"
}
}
</pre>the name A is used in an expression context to refer to the local variable A and in a type context to refer to the class A. end note]</span> </span><span class="ruler"></span><table><tr><td><table align="left" bgcolor="navy"><tr bgcolor="navy"><td><font face="Arial,sans-serif" size="6" color="yellow"><strong>{ JSL }</strong></font></td></tr></table></td></tr><tr><td><font face="Arial,sans-serif" size="2" color="navy"><strong>Jagger Software Ltd</strong></font></td></tr><tr><td><font face="Arial,sans-serif" size="2" color="navy"><strong>Company # 4070126</strong></font></td></tr><tr><td><font face="Arial,sans-serif" size="2" color="navy"><strong>VAT # 762 5213 42</strong></font></td></tr></table><img src="valid-html401.png" align="left" height="31" width="88" alt="Valid HTML 4.01" /><img src="vcss.gif" align="left" height="31" width="88" alt="Valid CSS" /></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -