C#变量范围 [英] C# Variable Scoping

查看:194
本文介绍了C#变量范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

string var = "New VAR!";

这会导致:

错误1的局部变量命名为无功
  不能在此范围中声明
  因为它会给出不同
  意思是无功,这已经是
  一个'孩子'范围来表示
  别的东西。

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?

我不能,如果外面打电话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.

推荐答案

这里的问题是很好的做法,防止意外的失误preventing很大程度上之一。诚然,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";
}

都处理precisely中的变量和范围方面是相同的。

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!";
}

然而,这同时有效的编译器,可以读取code时导致混乱的一定量的,所以我建议反对在几乎任何情况。

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#变量范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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