Because string is immutable.
The delegate running in ForEach is being passed a reference by value to your
string. You are overwriting that reference value to a new instance of a
string, leaving the original one intact.
In this case, it would be simpler to just use a for loop...
for( int i = 0; i < results.Length; i++ ) {
results[i] = results[i].Replace("¬", messageNumber.ToString());
}
If you're absolutely dying to use the anonymous method syntax, you can use
ConvertAll, but it's awkward to convert a string list to a string list.
results.ConvertAll<string>(
delegate( string input ) {
return input.Replace("¬", messageNumber.ToString());
}
);
Adam..
-----Original Message-----
From: Discussion of advanced .NET topics.
[mailto:[EMAIL PROTECTED] On Behalf Of Paul Cowan
Sent: Thursday, March 29, 2007 10:09 AM
To: [email protected]
Subject: [ADVANCED-DOTNET] Annonymous delegate question
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?
Cheers
Paul
===================================
This list is hosted by DevelopMentor® http://www.develop.com
View archives and manage your subscription(s) at http://discuss.develop.com
===================================
This list is hosted by DevelopMentor® http://www.develop.com
View archives and manage your subscription(s) at http://discuss.develop.com