Session object changes when object is updated in C#

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 + "ListModel"] = listModel;
listModel.ProductRows = new Collection<ProductRow>(listModel.ProductRows.Where(r => r.ParentRowId == 0).ToList());

After the second line is executed my session object is updated as well (according to “Watch” in Visual Studio)

What am I missing here?

I have tried

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

and HttpContext.Current.Session[“i”] remains 0.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

See value types and reference types.

The int is a value type so will be stored “as-is” on the moment of assignment; your listModel is a reference type so you store a reference to the object in your session, not the value of the object.

You’ll have to create a new instance of listModel if you want the one in your session untouched.

Method 2

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

Method 3

In the first example your session variable is pointing to a reference so it gets updated because the two references are pointing to the same value.

The second session variable is pointing to a primitive (value) type so they have separate copies of value.

Method 4

In the first example your session variable is pointing to a reference so it gets updated because the two references are pointing to the same value.

so before assigning to the session you convert it into Json and then assign

HttpContext.Current.Session[listModelType + "ListModel"] = JsonConvert.SerializeObject(listModel);

Note: JsonConvert is from name space Newtonsoft.Json namespace in c#

In second line if value changes in listModel object that does’t reflect session.
but when you want to retreive the value from session you should convert to object form Json

if (HttpContext.Current.Session[listModelType + "ListModel"] != null)
{

    listModel = JsonConvert.DeserializeObject<*CLASS name of lsitmodel*>((string)HttpContext.Current.Session[listModelType + "ListModel"]);

}

Method 5

The correct way should be:

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

the HttpContext.Current.Session[“i”] remains 1.

Method 6

In the first example your session variable is pointing to a reference so it gets updated because the two references are pointing to the same value.

so before assigning to the session you convert it into Json and then assign

   HttpContext.Current.Session[listModelType + "ListModel"] = JsonConvert.SerializeObject(listModel);

Note: JsonConvert is from name space Newtonsoft.Json namespace in c#

In second line if value changes in listModel object that does’t reflect session. but when you want to retreive the value from session you should convert to object form Json

   if (HttpContext.Current.Session[listModelType + "ListModel"] != null) {

        listModel = JsonConvert.DeserializeObject<*CLASS name of lsitmodel*>((string)HttpContext.Current.Session[listModelType + "ListModel"]);

    }


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x