ForEach
Shortcut for foreach and create new list
Source
public static IEnumerable<T> ForEach<T>(this IEnumerable<T> array, Action<T> act)
{
foreach (var i in array)
act(i);
return array;
}
public static IEnumerable<T> ForEach<T>(this IEnumerable arr, Action<T> act)
{
return arr.Cast<T>().ForEach<T>(act);
}
public static IEnumerable<RT> ForEach<T, RT>(this IEnumerable<T> array, Func<T, RT> func)
{
var list = new List<RT>();
foreach (var i in array)
{
var obj = func(i);
if (obj != null)
list.Add(obj);
}
return list;
}
Example
string[] names = new string[] { "C#", "Java" };
names.ForEach(i => Console.WriteLine(i));
IEnumerable<int> namesLen = names.ForEach(i => i.Length);
namesLen.ForEach(i => Console.WriteLine(i));
Author: Pedram Jabbari
Submitted on: 24 mrt. 2013
Language: C#
Type: System.Collections.Generic.IEnumerable<T>
Views: 23988