The session object changes when the object is updated in C #

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

HttpContext.Current.Session[listModelType + "ListModel"] = listModel; listModel.ProductRows = new Collection<ProductRow>(listModel.ProductRows.Where(r => r.ParentRowId == 0).ToList()); 

After completing the second line, my session object is also updated (according to "Watch" in Visual Studio)

What am I missing here?

I tried

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

and HttpContext.Current.Session ["i"] remains 0.

+6
source share
6 answers

See value types and link types .

int is the type of value, so it will be stored as is at the time of assignment; your listModel is a reference type, so you store a reference to the object in your session, not to the value of the object.

You will need to create a new instance of listModel if you want the one that was in your session not to be touched.

+11
source

In the first example, you store a reference to an object (the location of the list cell). Therefore, if the list updated, it will be displayed in the session. This is a reference type.

In the second example, you use the value type:

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

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

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

You increment i , but the session still has a value of 0

+4
source

In the first example, your session variable points to reference , so it is updated because two links point to the same value.

The second session variable points to the primitive (value) , so they have separate copies of the value.

+1
source

In the first example, your session variable points to a link, so it is updated as two links point to the same value.

so before you assign a session, you convert it to Json and then assign

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

Note. JsonConvert from Newtonsoft.Json namespace namespace in C #

In the second line, if the value changes in the listModel object, which does not reflect the session. but when you want to get a value from a session, you have to convert it to a Json object form

 if (HttpContext.Current.Session[listModelType + "ListModel"] != null) { listModel = JsonConvert.DeserializeObject<*CLASS name of lsitmodel*>((string)HttpContext.Current.Session[listModelType + "ListModel"]); } 
+1
source

The right way:

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

HttpContext.Current.Session ["i"] remains 1.

0
source

In the first example, your session variable points to a link, so it is updated as two links point to the same value.

so before you assign a session, you convert it to Json and then assign

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

Note. JsonConvert from Newtonsoft.Json namespace namespace in C #

In the second line, if the value changes in the listModel object, which does not reflect the session. but when you want to get a value from a session, you have to convert it to a Json object form

  if (HttpContext.Current.Session[listModelType + "ListModel"] != null) { listModel = JsonConvert.DeserializeObject<*CLASS name of lsitmodel*>((string)HttpContext.Current.Session[listModelType + "ListModel"]); } 
0
source

Source: https://habr.com/ru/post/948765/


All Articles