If I want to do the following:
List<string> listOfStrings = new List<string>();
listOfStrings.Add("13");
listOfStrings.Add("14");
listOfStrings.Add("FooBar");
listOfStrings.Add("Dummy14Item");
listOfStrings.Add("Brontosaurus165LT");
for (int i = 0; i < listOfStrings.Count; i++)
{
if (listOfStrings[i].EndsWith("LT"))
listOfStrings[i] = listOfStrings[i].Replace("LT", "");
}
So is there a way to make a projection of the List<t> that contains essentially the same items except for the modification? I tried
listOfStrings.ForEach(x => { if (x.EndsWith("LT")) x = x.Replace("LT", ""); });
But, unless I am doing something incorrectly, it doesn't seem to be working. One reason may be because the
ForEach
function takes as its parameter an
Action<T>
delegate, which passes
T obj
by value, not by reference.
Does anyone have a hint? I just hate the first block with the
for(int i;i < list.Count; i++ )
, it seems so gradeschool.
Brian