为什么我不能声明具有已经声明的相同名称的变量,但是新变量超出了其他变量的范围

Har*_*hna 2 c# foreach scope

private void Abc()
{
    string first="";
    ArrayList result = new ArrayList();
    ArrayList secResult = new ArrayList();
    foreach (string second in result)
    {
        if (first != null)
        {
            foreach (string third in secResult)
            {
                string target;
            }
        }

        string target;//Here I cannot decalre it. And if I don't declare it and
        //want to use it then also I cannot use it. And if it is in if condition like
        //the code commented below, then there is no any complier error.
        //if (first != null)
        //{
        //    string target;
        //}
    }
}
Run Code Online (Sandbox Code Playgroud)

我无法理解:为什么我不能在foreach循环外声明变量,因为编译器会给出一个已经声明变量的错误.我foreach(以及因此target变量)的范围已经结束,我宣布这个新变量.

Jon*_*eet 9

局部变量的范围一直延伸到声明它的块的开头.所以你的第二个声明的范围实际上是整个外部foreach循环.从C#4规范,第3.7节:

在local-variable-declaration(第8.5.1节)中声明的局部变量的范围是声明发生的块.

在第8.5.1节中:

在local-variable-declaration中声明的局部变量的范围是声明发生的块.在局部变量的local-variable-declarator之前的文本位置引用局部变量是错误的.在局部变量的范围内,声明另一个具有相同名称的局部变量或常量是编译时错误.

因此,即使第二个变量尚未在第一个变量发生的位置声明,它仍然在范围内 - 因此它们之间的两个声明违反了8.5.1.

这种语言的设计是为了防止错误 - 如果只是在声明它的块中移动局部变量声明的位置并在第一次使用之前改变代码的有效性,那就太奇怪了.