Executes a function on each item in IList and returns the results in a new IList. - CSharp System.Collections.Generic

CSharp examples for System.Collections.Generic:IList

Description

Executes a function on each item in IList and returns the results in a new IList.

Demo Code


using System.Collections.Generic;
using System.Collections;

public class Main{
        /// <summary>
        /// Executes a function on each item in a <see cref="IList{T}" />
        /// and returns the results in a new <see cref="IList{TResult}" />.
        /// </summary>
        /// <param name="list"></param>
        /// <param name="function"></param>
        /// <returns></returns>
        public static IList<TResult> Transform<T, TResult>(IList<T> list, FunctionDelegate<T, TResult> function)
        {/*from  ww w .  j av  a2 s  . co  m*/
            IList<TResult> result = new List<TResult>(list.Count);
            foreach (T item in list)
                result.Add(function(item));
            return result;
        }
        /// <summary>
        /// Executes a function on each item in a <see cref="IList{T}" />
        /// and returns the results in a new <see cref="IList{T}" />.
        /// </summary>
        /// <param name="list"></param>
        /// <param name="function"></param>
        /// <returns></returns>
        public static IList<T> Transform<T>(IList<T> list, FunctionDelegate<T> function)
        {
            IList<T> result = new List<T>(list.Count);
            foreach (T item in list)
                result.Add(function(item));
            return result;
        }
        /// <summary>
        /// Executes a function on each item in a <see cref="ICollection" />
        /// and returns the results in a new <see cref="IList" />.
        /// </summary>
        /// <param name="coll"></param>
        /// <param name="func"></param>
        /// <returns></returns>
        public static IList Transform(ICollection coll, FunctionDelegate<object> func)
        {
            IList result = new ArrayList();
            foreach(object obj in coll)           
                result.Add(func(obj));
            return result;
        }
}

Related Tutorials