F# List.map equivalent in C#?

Viewed 86682

Is there an equivalent to F#'s List.map function in C#? i.e. apply a function to each element in the list and return a new list containing the results.

Something like:

    public static IEnumerable<TResult> Map<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, TResult> funky)
    {
        foreach (TSource element in source)
            yield return funky.Invoke(element);
    }

Is there already a built in way or should I just write the custom extension?

3 Answers

To simplify what @codeape said:

List<string> result = ListWithAnyObjects.ConvertAll<string>(obj => obj.ToString());
Related