I am getting into infinite loop in property setter

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


        
4条回答
  •  有刺的猬
    2020-12-06 07:44

    There's nothing particularly string-like about session state items.

    Why wouldn't you just follow the KISS principle and do something like

    public int Position
    {
      get { return (int) ( Session["Position"] ?? 5 ) ; }
      set { Session["Position"] = value ;               }
    }
    

    or (depending on your actual requirements/specs:

    public int Position
    {
      get { return Session["Pointer"] as int? ?? position ?? 5 ; }
      set { position = value ; }
    }
    private int? position ; // backing store
    

提交回复
热议问题