C#变量作用域:无法在此范围内声明“ x”,因为它将赋予“ x”不同的含义 [英] C# variable scoping: 'x' cannot be declared in this scope because it would give a different meaning to 'x'

查看:161
本文介绍了C#变量作用域:无法在此范围内声明“ x”,因为它将赋予“ x”不同的含义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

if(true)
{
    string var = "VAR";
}

string var = "New VAR!";

这将导致:


错误1无法在此范围
中声明名为'var'
的局部变量,因为它将赋予'var'一个不同的
含义,该变量已经是
在儿童范围内用于表示
的其他内容。

Error 1 A local variable named 'var' cannot be declared in this scope because it would give a different meaning to 'var', which is already used in a 'child' scope to denote something else.

没有什么东西可以真正地粉碎,但不是这只是明显的错误?我和一位开发人员同行,我想知道第一个声明是否应该在不同的范围内,因此第二个声明不能干扰第一个声明。

Nothing earth shattering really, but isn't this just plain wrong? A fellow developer and I were wondering if the first declaration should be in a different scope, thus the second declaration cannot interfere with the first declaration.

为什么C#无法区分这两个作用域?应该不会将第一个IF范围与该方法的其余部分完全分开吗?

Why is C# unable to differentiate between the two scopes? Should the first IF scope not be completely separate from the rest of the method?

我无法从if外部调用var,因此错误消息是错误的,因为第一个var与第二个范围无关。

I cannot call var from outside the if, so the error message is wrong, because the first var has no relevance in the second scope.

推荐答案

此处的问题在很大程度上是一种良好的做法,可以防止无意中的错误。诚然,C#编译器理论上可以设计为在这里范围之间没有冲突。但是,如我所见,这会付出很大的努力,却几乎没有收益。

The issue here is largely one of good practice and preventing against inadvertent mistakes. Admittedly, the C# compiler could theoretically be designed such that there is no conflict between scopes here. This would however be much effort for little gain, as I see it.

考虑一下,如果声明 var 在父级范围内,如果在 before if语句之前,将存在无法解决的命名冲突。编译器根本不会区分以下两种情况。分析是纯粹基于范围完成的,而不是您似乎期望的声明/使用顺序。

Consider that if the declaration of var in the parent scope were before the if statement, there would be an unresolvable naming conflict. The compiler simply does not differentiate between the following two cases. Analysis is done purely based on scope, and not order of declaration/use, as you seem to be expecting.

理论上可以接受(但就C#而言仍然无效):

The theoretically acceptable (but still invalid as far as C# is concerned):

if(true)
{
    string var = "VAR";
}

string var = "New VAR!";

和不可接受的(因为它将隐藏父变量):

and the unacceptable (since it would be hiding the parent variable):

string var = "New VAR!";

if(true)
{
    string var = "VAR";
}

在变量和范围方面都完全相同。

are both treated precisely the same in terms of variables and scopes.

现在,在此secenario中有什么实际原因为什么您不能仅仅给变量之一起不同的名字?我假设(希望)您的实际变量未称为 var ,所以我并不认为这是个问题。如果您仍想重用相同的变量名,只需将它们放在同级作用域中即可:

Now, is there any actual reason in this secenario why you can't just give one of the variables a different name? I assume (hope) your actual variables aren't called var, so I don't really see this being a problem. If you're still intent on reusing the same variable name, just put them in sibling scopes:

if(true)
{
    string var = "VAR";
}

{
    string var = "New VAR!";
}

这虽然对编译器有效,但可能导致一定程度的混乱在阅读代码时,所以我几乎建议在任何情况下都反对它。

This however, while valid to the compiler, can lead to some amount of confusion when reading the code, so I recommend against it in almost any case.

这篇关于C#变量作用域:无法在此范围内声明“ x”,因为它将赋予“ x”不同的含义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆