27.9.09

C# Map function for Generic Lists

I whipped up a quick map function for applying a modifying function to all items in a list, and potentially changing the type of each item:
public static class ListMap
{
public static List Map(List list, Func mapFunction)
{
if (list == null)
{
throw new ArgumentNullException("list");
}

if (mapFunction == null)
{
throw new ArgumentNullException("mapFunction");
}

List outputList = new List();
list.ForEach(t => outputList.Add(mapFunction(t)));
return outputList;
}
}

Usage:
List stringList = new List(new string[] { "1", "2", "3" });
List result = ListMap.Map(stringList, x => int.Parse(x));

This results in a List containing {1, 2, 3}.