简体   繁体   English

迭代器变量超出C#的范围

[英]Iterator variable get out of scope in C#

Why if I write this in C#: 为什么我用C#写这个:

for(int myVar = 0; myVar<10; myVar++)
{
//do something here
}

//then, if I try:
int myVar = 8;
//I get some error that the variable is already declared.

//but if I try:
Console.WriteLine(myVar);
//then I get the error that the variable is not declared.

A little confusing I most say. 我最常说的有点混乱。 Does anyone know why C# compiler does it? 有谁知道为什么C#编译器会这样做?

This rule, along with several other related rules, is discussed in this blog post of Eric Lippert's . 此规则以及其他一些相关规则将在Eric Lippert的博客文章中进行讨论。

The particular rule being violated here is: 这里违反的特定规则是:

2) It is illegal to have two local variables of the same name in the same local variable declaration space or nested local variable declaration spaces. 2)在同一局部变量声明空间或嵌套局部变量声明空间中有两个同名的局部变量是非法的。

Of particular note, as to why the rules exist is the following paragraph: 特别值得注意的是,关于规则存在的原因如下:

the purpose of all of these rules is to prevent the class of bugs in which the reader/maintainer of the code is tricked into believing they are referring to one entity with a simple name, but are in fact accidentally referring to another entity entirely. 所有这些规则的目的是防止一类错误,其中代码的读者/维护者被欺骗相信他们指的是一个具有简单名称的实体,但实际上是在意外地完全引用另一个实体。 These rules are in particular designed to prevent nasty surprises when performing what ought to be safe refactorings. 这些规则特别设计用于在执行应该是安全的重构时防止令人讨厌的意外。

By allowing what you've described it could result in seemingly benign refactors, such as moving the for loop to after the other declaration, to result in vastly different behavior. 通过允许你所描述的内容,它可以产生看似良性的重构,例如将for循环移动到另一个声明之后,导致极其不同的行为。

A local variable named 'myVar' cannot be declared in this scope because it would give a different meaning to 'myVar', which is already used in a 'child' scope to denote something else. 名为“myVar”的局部变量不能在此范围内声明,因为它会为“myVar”赋予不同的含义,“myVar”已在“子”范围中用于表示其他内容。

Both variables are declared in the same scope. 两个变量都在同一范围内声明。 (One in a child scope which still won't let you to declare it twice) You can't declare the variable a second time. (一个在子范围内仍然不允许您声明它两次)您不能再次声明该变量。
Changing the scope will allow you to declare it a second time. 更改范围将允许您第二次声明它。

private void myMethod()
{
   //Start of method scope 

   for (int myVar = 0; myVar < 10; myVar ++)
   {
      //Start of child scope
      //End of child scope
   }

   //End of method scope
}

private void anotherMethod()
{
   // A different scope 
   int myVar = 0 
}

The error you're referring to as already declared is quite descriptive and says... 您所指的already declared的错误是非常具有描述性的,并说......

A local variable named 'myVar' cannot be declared in this scope because it would give a different meaning to 'myVar', which is already used in a 'child' scope to denote something else 名为'myVar'的局部变量不能在此范围内声明,因为它会给'myVar'赋予不同的含义,'myVar'已在'child'范围内用于表示其他内容

myVar is actually not 'declared` when you try to use it the second time. 当你第二次尝试使用时, myVar实际上并没有'声明'。

Looking very strange, but C# offers you to seperate some code lines by simply using some curly brackets like this: 看起来很奇怪,但是C#提供了一些代码行,只需使用一些大括号,如下所示:

for(int myVar = 0; myVar<10; myVar++)
{
//do something here
}

{
   //then, if I try:
   int myVar = 8;
   //I get some error that the variable is already declared.

   //but if I try:
   Console.WriteLine(myVar);
   //then I get the error that the variable is not declared.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM