ExtensionMethod.NET Home of 819 C#, Visual Basic, F# and Javascript extension methods

Convert

Converts from one type to another.

Source

public static IEnumerable<TDestination> Convert<TSource, TDestination>(this IEnumerable<TSource> enumerable, Func<TSource, TDestination> converter)
{
    if (enumerable == null)
    {
        return null;
    }

    List<TDestination> items = new List<TDestination>();

    foreach (TSource item in enumerable)
    {
        items.Add(converter(item));
    }

    return items.ToArray();
}

Example

int[] integers = new int[] { 5, 25, 50, 100 };
string[] strings = integers.Convert(i => i.ToString());

Author: Adam Weigert

Submitted on: 3 mrt 2008

Language: C#

Type: System.Collections.Generic.IEnumerable<T>

Views: 3470