如何.NET处理范围内的变量 [英] How does .NET handle variables inside scope

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

问题描述

我对什么happning幕后只是couris。我有这个code,当然它不会编译,因为我创造了招呼变量if语句内,稍后尝试声明它agian。为什么忽略了最低.NET允许我这样做?请告诉我happning,可以使招呼变量interfeer同一个语句中的scences后面。

这是非常stright前锋,为什么这可能interfeer如果变量在if语句之前声明。

 公共无效测试(){
        如果(真)
        {
            VAR你好=;
        }
        VAR你好=;

        Console.Write(你好);
    }
 

解决方案

我想弄清楚,这里有违反两个规则。

首先是嵌套的局部变量声明空间可能不包含两个同名的声明。

二是嵌套的局部范围可能不包含相同的简单的名称声明的两种用法的意味着两个不同的东西。

这两个规则被违反。注意,第一规则基本上是第二个规则的一个特例。第二条规则是更普遍的;例如,这也违反了:

  C类
{
    INT X;
    无效M()
    {
        如果(无论)
        {
            字符串x =;
        }
        X = 10;
    }
}
 

下面的简单名称 X 用来表示 this.x 在一个局部范围和局部变量 X 在嵌套范围。这是混乱的;一个简单的名字被要求意味着在其整个块的同样的事情。因此,我们是非法。这将是合法的:

  C类
{
    INT X;
    无效M()
    {
        如果(无论)
        {
            字符串x =;
        }
    }
}
 

即使本地变量在嵌套领域范围内的范围内声明的,这是允许的,因为该领域的范围是不是一个局部变量声明空间。

这也是合法的:

  C类
{
    INT X;
    无效M()
    {
        如果(无论)
        {
            字符串x =;
        }
        其他
        {
            X = 2;
        }
    }
}
 

因为现在的这两个相互矛盾的用法X 都始终在他们的直接包含范围的法律约束条款使用。那些范围,现在不重叠,所以这是允许的。 (它仍然是一个坏主意,但是。)

这些规则在那里为您的安全,但他们可能会相当混乱。请参见

http://blogs.msdn.com/b/ericlippert /存档/标签/报关+空格/

http://blogs.msdn.com/b/ericlippert/archive /标签/范围/

有关这些语言特性的文章。

I'm just couris about whats happning behind the scenes. I have this code and of course it will not compile cause I create the hello variable inside a if statement and later try to declare it agian. Why dosen't .NET allow me to do so? Whats happning behind the scences that could make the hello variable interfeer with the one inside the statement.

It's very stright forward why this could interfeer if the variable was declared before the if statement.

    public void Test() {
        if (true)
        {
            var hello = "";
        }
        var hello = "";

        Console.Write(hello);
    }

解决方案

Just to clarify, there are two rules violated here.

The first is that nested local variable declaration spaces may not contain two declarations of the same name.

The second is that nested local scopes may not contain two usages of the same simple name or declaration to mean two different things.

Both rules are violated. Note that the first rule is essentially a special case of the second rule. The second rule is more general; for example, this is also a violation:

class C
{
    int x;
    void M()
    {
        if (whatever)
        {
            string x = "";
        }
        x = 10;
    }
}

Here the simple name x is used to mean this.x in one local scope and the local variable x in a nested scope. This is confusing; a simple name is required to mean the same thing throughout its entire block. We therefore make it illegal. This would be legal:

class C
{
    int x;
    void M()
    {
        if (whatever)
        {
            string x = "";
        }
    }
}

Even though the local variable is declared in a scope nested inside the scope of the field, this is allowed because the field's scope is not a local variable declaration space.

This is also legal:

class C
{
    int x;
    void M()
    {
        if (whatever)
        {
            string x = "";
        }
        else
        {
            x = 2;
        }
    }
}

because now the two conflicting usages of x are both used consistently throughout the entirity of their immediately containing scopes. Those scopes now do not overlap, so this is allowed. (It is still a bad idea however.)

These rules are there for your safety but they can be quite confusing. See

http://blogs.msdn.com/b/ericlippert/archive/tags/declaration+spaces/

and

http://blogs.msdn.com/b/ericlippert/archive/tags/scope/

for some articles on these language features.

这篇关于如何.NET处理范围内的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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