var arr = new[] {"A ", "B ", "C "}.ToList();
arr.ForEach(a => a = a.Replace(" ", ""));
Why this does not remove space characters from the strings in the array?
This works arr = arr.Select(a => a.Replace(" ", "")).ToList();
The problem is that a.Replace(..) returns a (reference to) a new string. You do assign that new reference back to the local parameter a. However, that parameter a is a copy of the reference in the list. Updating a does not update the reference in the list itself.
If you want to update "in place", you will have to do it the old fashioned way:
for (var i=0; i<arr.Count; i++)
{
arr[i] = arr[i].Replace(" ", "");
}
arr = arr.Select(a => a.Replace(" ", "")).ToList();arr = arr.Select(a => a.Replace(" ", ""))which is hideous. Sorry about that, you are not mutating anything, simply doing a projection and reassigningarrwhich is the right apporach..ToList(). The .ForEachmethod is just a method onList<>.