ForEach

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));

Description

Shortcut for foreach and create new list

Details

Double click on the code to select all.

 

;