问题描述
例如:
if ( this.IsValid )
{
Matrix matrix = new Matrix();
}
Matrix matrix = new Matrix();
编译器警告我说:
不能在此范围内声明名为'matrix
'的局部变量,因为它会给'matrix
'赋予不同的含义,后者已在'中使用'child' 范围来表示其他东西.
"A local variable named 'matrix
' cannot be declared in this scope because it would give a different meaning to 'matrix
', which is already used in a 'child' scope to denote something else.
这些变量不是在不同的范围内,所以无论如何我都无法从 if 语句之外访问第一个 matrix
吗?
Aren't these variables in different scopes, so I wouldn't be able to access the first matrix
from outside the if statement anyway?
推荐答案
更新:以下 2011 年的答案对于早期版本的 C# 是正确的;在最近的版本中,描述答案的规则已从 C# 中删除.设计团队确定,即使在我大大改进了错误消息以更清楚地诊断问题之后,该规则也会导致开发人员产生更多的混乱,从而导致出现此类问题,而不是阻止错误程序所保证的情况.
UPDATE: The answer below from 2011 is correct for earlier versions of C#; in more recent versions, the rule described the answer has been removed from C#. The design team determined that the rule caused more confusion amongst developers leading to questions like this one than the buggy programs prevented would warrant, even after I greatly improved the error messages to more clearly diagnose the problem.
到目前为止给出的答案非常令人困惑.对问题的正确分析始于阅读错误信息.错误消息告诉您实际上是什么错误:
The answers given so far are very confusing. The correct analysis of the problem starts by reading the error message. The error message is telling you what is actually wrong:
不能在此范围内声明名为'matrix'的局部变量,因为它会给'matrix'赋予不同的含义,而'matrix'已在'子'范围内用于表示其他内容.
"A local variable named 'matrix' cannot be declared in this scope because it would give a different meaning to 'matrix', which is already used in a 'child' scope to denote something else.
仔细阅读.它准确地告诉您违反了 C# 的哪条规则,即不允许在同一范围内使用相同的名称来引用两个不同的事物.(实际上,错误信息有点错误;应该说局部变量声明空间",它说范围",但这很罗嗦.)
Read that carefully. It is telling you precisely which rule of C# is being violated, namely that you are not allowed to use the same name to refer to two different things in the same scope. (Actually, the error message is slightly wrong; it should say "local variable declaration space" where it says "scope", but that is pretty wordy.)
此规则记录在 C# 4.0 规范的第 7.6.2.1 节:简单名称,块中的不变含义.
This rule is documented in the C# 4.0 specification, section 7.6.2.1: Simple names, Invariant meaning in blocks.
(也在重叠的声明空间中有两个同名的局部变量是非法的.编译器也可能报告该错误,但在这种情况下它会报告更一般的错误.)
(It is also illegal to have two local variables of the same name in overlapping declaration spaces. The compiler could be reporting that error as well, but it reports the more general error in this case.)
这些变量不是在不同的范围内,所以我无论如何都无法从 if 语句之外访问第一个矩阵?
Aren't these variables in different scopes, so I wouldn't be able to access the first matrix from outside the if statement anyway?
是的.该陈述是正确,但无关.这里的错误是在同一个局部变量声明空间中使用了相同的简单名称来引用两个不同的东西.
Yes. That statement is true but irrelevant. The error here is that the same simple name has been used to refer to two different things in the same local variable declaration space.
考虑这种情况:
class C
{
int x;
void M()
{
x = 10; // means "this.x"
for(whatever)
{
int x = whatever;
}
}
}
同样的交易.这里的错误是在外部声明空间中使用简单名称x"来引用this.x,而在内部声明空间中使用简单名称x"来表示局部变量".在同一个声明空间中使用相同的简单名称来引用两个不同的事物——记住,内部声明空间是外部声明空间的部分——两者都混淆并且危险,因此是非法的.
Same deal. The error here is that the simple name "x" was used in the outer declaration space to refer to this.x, and was used in the inner declaration space to mean "local variable". Using the same simple name to refer to two different things in the same declaration space -- remember, the inner declaration space is a part of the outer one -- is both confusing and dangerous, and is therefore illegal.
由于显而易见的原因,它令人困惑;人们有一个合理的期望,即一个名称在第一次使用它的声明空间中的任何地方都意味着相同的东西.这很危险,因为小的代码编辑容易改变含义:
It is confusing for obvious reasons; one has a reasonable expectation that a name will mean the same thing everywhere throughout the declaration space in which it is first used. It is dangerous because small code edits are prone to changing the meaning:
class C
{
int x;
void M()
{
int x;
x = 10; // no longer means "this.x"
for(whatever)
{
x = whatever;
}
}
}
如果第一次使用简单名称的声明空间不重叠,那么简单名称引用不同的东西是合法的:
If the declaration spaces in which the simple names are first used are not overlapping then it is legal for the simple names to refer to different things:
class C
{
int x;
void M()
{
{
x = 10; // means "this.x"
}
for(whatever)
{
int x = whatever; // Legal; now the
}
}
}
如需了解更多信息以及有关油炸食品的有趣故事,请参阅
For more information, and an amusing story about fried food, see
http://blogs.msdn.com/b/ericlippert/archive/tags/simple+names/
这篇关于为什么 C# 不允许我在不同的范围内使用相同的变量名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!