I am getting into infinite loop in property setter

前端 未结 4 652
盖世英雄少女心
盖世英雄少女心 2020-12-06 06:57
public int Position
{
    get
    {
        if (Session[\"Position\"] != null)
        {
            Position = Convert.ToInt32(Session[\"Position\"]);
        }
            


        
4条回答
  •  死守一世寂寞
    2020-12-06 08:02

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

    Correct code would be

    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;
        }
    }
    

提交回复
热议问题