how to modify items in a Generic List using foreach?
Asked Answered
H

2

8

I have the following Generic List which is populated with a list of string:

List<string> mylist =new List<string>();  
myList.add("string1");  
myList.add("string2");

Say I want to add 'test' at the end of each string, how can I do it in a simple way? Intuitively, I tried this which compiles ok:

myList.ForEach(s => s = s + "test");  

But if I then look at the content of the List, nothing has changed. I guess I could use a for loop to iterate through the List but I'm looking for something very simple and using ForEach looks very neat.... but doesn't seem to work. Any ideas?

Homemaking answered 7/6, 2009 at 11:3 Comment(0)
H
15

The problem is that the Action you specified gets executed on the elements of the list, but the result is not put back anywhere... your s is a local variable only.

Changing the list in-place will probably take an actual foreach, but if you are happy to take a new list as the result, you could try:

list = list.ConvertAll(s => s + "test");

Not quite the same... but as close as you'll get...

Hyperbole answered 7/6, 2009 at 11:13 Comment(0)
E
9

It's not possible to do that unless the list type is a mutable reference type (and in that case, you can't still change the actual reference in the list but the object itself).

The reason is that List<T>.ForEach calls a Action<T> delegate with signature:

delegate void Action<T>(T obj);

and here, the argument is passed by value (it's not ref). Like any method, you can't change the input argument when it's called by value:

The code is essentially equivalent to:

void anonymous_method(string s) {
    s = s + "test";  // no way to change the original `s` inside this method.
}

list.ForEach(anonymous_method);
Endowment answered 7/6, 2009 at 11:7 Comment(2)
I also thank ye' for the explanation... That's the simplest straigth-up explanation I've seen... beats anything and everything on MSDN hands down. Ta.Archy
Yes, I like this answer, much cleaner and intuitiveVisually

© 2022 - 2024 — McMap. All rights reserved.