Session object changes when object is updated in C#

后端 未结 6 1433
执笔经年
执笔经年 2020-12-11 05:28

I have this really weird problem and I\'m sure I\'m missing something obvious here. I have these two lines:

HttpContext.Current.Session[listModelType + \"Lis         


        
6条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-11 05:42

    In your first example you are storing a reference to the object (The lists memory location). So if the list is updated it will reflect in the session. This is a reference type.

    In the second example you are using a value type:

    int i = 0;
    HttpContext.Current.Session["i"] = i;
    i++;
    

    You declare i and set it to 0 (Value type)

    You store the value 0 in the session. (Not the memory location of i)

    You increment i but the session still has the value 0

提交回复
热议问题