Hi,

I have the following code running in a

results.ForEach(
   delegate(string s)
   {
       s = s.Replace("¬", messageNumber.ToString());
   }
);

Results is a generic List<>

After the ForEach has executed, the elements of the results List<> have
not changed, I have to do this:

int index = 0;
results.ForEach(
   delegate(string s)
   {
       results[index] = s.Replace("¬", messageNumber.ToString());
       index++;
   }
);

Can anyone explain to me why this is?

strings are immutable, so every time you call s.Replace, it creates a new
string rather than overwriting the original. Assigning back to 's'
doesn't help as 's' is simply a local variable (a copy of the
reference to the actual string) and not the list item.

John

===================================
This list is hosted by DevelopMentor®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to