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

查看:15
本文介绍了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的声明在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.

现在,在这个场景中是否有任何实际原因,为什么您不能只为其中一个变量指定一个不同的名称?我假设(希望)您的实际变量不称为 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天全站免登陆