I have a list like:
List<String> test = new List<String> {"Luke", "Leia"};
I would like to use something like this:
test.Select(s => String.Format("Hello {0}", s));
but it doesn’t adjust the names in the list. Is there a way to use lambda expressions to alter these? Or is it because strings are immutable that this doesn’t work?
Select doesn’t modify the original collection; it creates a new IEnumerable<T> that you can enumerate with a foreach or convert to a list:
test still contains “Luke” and “Leia”, and test2 contains “Hello Luke” and “Hello Leia”.
If you want to modify the original list with a lambda expression, you can apply the lambda expression to each list item individually and store the result back in the collection: