Executes a function on each item in ICollection and collects all the entries for which the result of the function is equal to true. - CSharp System.Collections

CSharp examples for System.Collections:ICollection

Description

Executes a function on each item in ICollection and collects all the entries for which the result of the function is equal to true.

Demo Code


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

public class Main{
        /// <summary>
        /// Executes a function on each item in a <see cref="ICollection" />
        /// and collects all the entries for which the result
        /// of the function is equal to <c>true</c>.
        /// </summary>
        /// <param name="items"></param>
        /// <param name="func"></param>
        /// <returns></returns>
        public static IList<T> Select<T>(IEnumerable<T> items, FunctionDelegate<T, bool> func)
        {//from  w w  w .  ja  v a 2  s.c o  m
            IList<T> result = new List<T>();
            foreach (var obj in items)
                if (func(obj)) result.Add(obj);
            return result;
        }
        /// <summary>
        /// Executes a function on each item in a <see cref="ICollection" />
        /// and collects all the entries for which the result
        /// of the function is equal to <c>true</c>.
        /// </summary>
        /// <param name="coll"></param>
        /// <param name="func"></param>
        /// <returns></returns>
        public static IList Select(ICollection coll, FunctionDelegate<object, bool> func)
        {
            IList result = new ArrayList();            
            foreach (object obj in coll)
                if (func(obj))
                    result.Add(obj);                            
            return result;
        }
}

Related Tutorials