Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Application level variable in WPF

Tags:

c#

wpf

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?

like image 390
uncia Avatar asked Jul 24 '26 17:07

uncia


1 Answers

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();
like image 56
Habib Avatar answered Jul 27 '26 06:07

Habib