C # - using a copy of variables stored in a session instead of a link

I have asp: ImageButton with OnClick = "Btn_OnClick".
In Btn_OnClick, I have this line:

DataTable dtTable = (DataTable)Session["someSessionKey"]

and dtTable changes in function.

I noticed that if the button is pressed more than once, the dtTable that I take from the session contains a modified table, maybe the dtTable value is not a copy, but a reference to the session variable.

How can I change the copy of the session ["someSessionKey"] and not the actual value?
Thank!

+3
source share
2 answers
DataTable dtTable = ((DataTable)Session["someSessionKey"]).Copy();
+5
source

ICloneable, var myCopy = mySessionObject.Clone();. , , .

, . , , , .

, MemberwiseClone(), System.Object, . , , .

List<T> IEnumerable<T>, var myCopiedList = new List<T>(myListInSession);. , . foreach . DataTable. , .

, - , . . , , , .

, , . Google : # Object clone wars.

+2

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


All Articles