I have an application level variable in one window
object temp1 = App.Current.Properties["listofstring"];
var temp2 = (List<string>)temp1;
when iam changing lets say
temp2[0]="abc";
it also change that in "listofstring"
so i made a copy
List<string> temp3 = temp2;
but if i do
temp3[0] ="abc";
it change in "listofstring" too when accessed in other window ?
How do i use only local copy of it not disturb its contents once declared?
You are not making copy of the list, instead you are copying the reference. You can do:
List<string> temp3 = new List<string>(temp2.ToArray());
//or
List<string> temp3 = new List<string>(temp2);
Or
List<string> temp3 = temp2.Select(r=>r).ToList();
//or
List<string> temp3 = temp2.ToList();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With