I am getting into infinite loop in property setter

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


        
4条回答
  •  误落风尘
    2020-12-06 07:57

    Use a member variable or perhaps store it in the session.

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

提交回复
热议问题