Find Element from IEnumerable by Predicate - CSharp System.Collections.Generic

CSharp examples for System.Collections.Generic:IEnumerable

Description

Find Element from IEnumerable by Predicate

Demo Code


using System.Collections.Generic;
using System.Collections;
using System;/*from  w w  w.  j av  a 2s.com*/

public class Main{
        public static T Find<T>(IEnumerable collection, Predicate<T> p)
        {
            foreach (T obj in collection)
            {
                if (p(obj))
                    return obj;
            }

            return default(T);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="collection"></param>
        /// <param name="p"></param>
        /// <returns></returns>
        public static T Find<T>(IEnumerable<T> collection, Predicate<T> p)
        {
            foreach (T obj in collection)
            {
                if (p(obj))
                    return obj;
            }

            return default(T);
        }
}

Related Tutorials