我进入在属性setter无限循环 [英] I am getting into infinite loop in property setter

查看:275
本文介绍了我进入在属性setter无限循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public int Position
{
    get
    {
        if (Session["Position"] != null)
        {
            Position = Convert.ToInt32(Session["Position"]);
        }
        else
        {
            Position = 5;
        }
        return Position;
    }
    set
    {
        Position = value;
    }
}

我的程序调用get和进入环,如果然后运行infitely成集code

my program calls the get and goes into if loop and then runs infitely into set code

推荐答案

这个错误是因为在你的集合{} 您递归调用相同的制定者。

The error is because in your set {} you are invoking the same setter recursively.

正确code将

private int _position;
public int Position
{
    get
    {
        if (Session["Position"] != null)
        {
            this._position = Convert.ToInt32(Session["Position"]);
        }
        else
        {
            this._position = 5;
        }
        return this._position;
    }
    set
    {
        this._position = value;
    }
}

这篇关于我进入在属性setter无限循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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