与实例变量同名的局部变量=意外结果 [英] Local variable with same name as instance variable = unexpected results

查看:137
本文介绍了与实例变量同名的局部变量=意外结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ASP.NET 4.0 Webforms项目.我的代码后面有以下内容.

ASP.NET 4.0 Webforms project. I have the following in my code-behind.

public partial class _Default : System.Web.UI.Page
{
    private string testVar;

    protected override void OnInit(EventArgs e)
    {
        string testVar = "test";
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        var whatsTheValue = testVar;
    }
}

我正在每种方法中设置一个断点.当在OnInit中设置了局部变量testVar时,如果我快速查看实例变量,它的值也将为"test".当我浏览Page_Load时,实例变量的值为null.

I'm setting a break point inside each method. When the local variable, testVar, is set in OnInit, if I quick watch the instance variable, it also has the value "test". When I play through to Page_Load, the instance variable's value is null.

我偶然遇到了这个问题,但是这种行为使我感到困惑.我真的很惊讶它可以编译.我本来希望看到有关两个具有相同名称的变量的警告.话虽这么说,令我更加困惑的是实例变量在OnInit中选择了赋值,但是在退出该方法后立即丢失了.

I ran across this by accident but the behavior is confusing to me. I'm actually surprised that it compiles. I would have expected to see some sort of warning about having two variables with the same name. That being said, it's even more confusing to me that the instance variable picks up the assignment in OnInit, but then immediately loses it when that method is exited.

有人可以向我解释这种行为吗?

Can someone explain this behavior to me?

推荐答案

如果我快速查看实例变量,它也具有值"test". 当我浏览到Page_Load时,实例变量的值为 空.

if I quick watch the instance variable, it also has the value "test". When I play through to Page_Load, the instance variable's value is null.

没有,您没有看到实例变量,而是看到了局部变量.永远不要设置实例变量,因为局部作用域变量在作用域生存期内会隐藏该实例变量.

You are not seeing the instance variable, you see the local variable. The instance variable is never set, because the locally scoped variable is hiding the instance variable during it's scope lifetime.

根据规格:

3.7.1隐藏姓名

与实体的声明空间相比,实体的范围通常包含更多的程序文本.特别是, 实体的范围可能包括引入新的声明 声明空间包含相同名称的实体. 这样 声明会导致原始实体被隐藏.反过来, 实体在不隐藏时被称为可见.

The scope of an entity typically encompasses more program text than the declaration space of the entity. In particular, the scope of an entity may include declarations that introduce new declaration spaces containing entities of the same name. Such declarations cause the original entity to become hidden. Conversely, an entity is said to be visible when it is not hidden.

隐藏姓名 当范围通过嵌套重叠并且范围重叠时发生 通过继承.两种类型的隐藏特征 在以下各节中进行了介绍.

Name hiding occurs when scopes overlap through nesting and when scopes overlap through inheritance. The characteristics of the two types of hiding are described in the following sections.

嵌套可能会导致通过嵌套隐藏名称 嵌套类型导致的名称空间或名称空间中的类型 在类或结构中,以及由于参数和局部 变量声明

Name hiding through nesting can occur as a result of nesting namespaces or types within namespaces, as a result of nesting types within classes or structs, and as a result of parameter and local variable declarations

这篇关于与实例变量同名的局部变量=意外结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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