tags:

views:

1628

answers:

2

Why Cant I do something like this?

If I have a List<String> myList populated with items, I want to be able to act on each member in a conditional way like so:

myList.ForEach(a => { if (a.Trim().Length == 0) a = "0.0"; })

But this will not compile. Im guessing its something to do with missing a return value?

Im trying to prepare a list of strings for conversion to doubles, and I want the empty items to show '0.0' so I can just convert the whole list in one go.

+4  A: 

ForEach is not mutable, it doesn't change the data structure in any way.

You can either:

  1. Handle the loop yourself, with an index
  2. Produce a new list, using .Select and .ToList (provided you're using .NET 3.5)

Example of #1:

for (Int32 index = 0; index < myList.Count; index++)
    if (myList[index].Trim().Length == 0)
        myList[index] = "0.0";

With .NET 3.5 and Linq:

myList = (from a in myList
          select (a.Trim().Length == 0) ? "0.0" : a).ToList();

With .NET 3.5, not using the Linq-syntax, but using the Linq-code:

myList = myList.Select(a => (a.Trim().Length == 0) ? "0.0" : a).ToList();


Edit: If you want to produce a new list of doubles, you can also do that in one go using Linq:

List<Double> myDoubleList =
    (from a in myList
     select (a.Trim().Length == 0 ? "0" : a) into d
     select Double.Parse(d)).ToList();

Note that using "0.0" instead of just "0" relies on the decimal point being the full stop character. On my system it isn't, so I replaced it with just "0", but a more appropriate way would be to change the call to Double.Parse to take an explicit numeric formatting, like this:

List<Double> myDoubleList =
    (from a in myList
     select (a.Trim().Length == 0 ? "0.0" : a) into d
     select Double.Parse(d, CultureInfo.InvariantCulture)).ToList();
Lasse V. Karlsen
Hmmm ok , is there any extension method that will allow you to act on each item in the list?
Alex
Not that I know of, and I doubt it, most of the Linq-methods are built in a functional style, which disallows changing the input in any way.
Lasse V. Karlsen
Thanks for the excellent explanation.
Alex
+2  A: 

What you possibly want is:

public static void MutateEach(this IList<T> list, Func<T, T> mutator)
{
    int count = list.Count;
    for (int n = 0; n < count; n++)
        list[n] = mutator(list[n]);
}

Which would allow:

myList.MutateEach(a => (a.Trim().Length == 0) ? "0.0" : a);

Just put the MutateEach method in a public static class of your own and make sure you have a using directive that will find it.

Whether it's worth defining something like this depends on how often you'd use it. Note that it has to be an extension on IList instead of IEnumerable, so we can perform the updates, which makes it less widely applicable.

Daniel Earwicker
+1 for naming, which would make this a good extension.
Lasse V. Karlsen
You guys are fantastic. I learn something every time I ask a question on this site.
Alex