CSharp examples for System.Collections.Generic:IEnumerable
Finds the first element in the collection that satisfies the predicate
using System.Linq; using System.Collections.Generic; using System.Collections; using System;/*from w ww . jav a 2 s . co m*/ public class Main{ /// <summary> /// Finds the first element in the collection that satisfies the predicate /// </summary> /// <typeparam name="T">Type of the predicate</typeparam> /// <param name="collection">Collection to search</param> /// <param name="predicate">Predicate to satisfy</param> /// <returns>The first element to satisfy the predicate</returns> /// <remarks>If the predicate can not be satisfied default(T) is returned</remarks> public static T Find<T>(this IEnumerable collection, Predicate<T> predicate) { return collection.Cast<T>().Find(predicate); } /// <summary> /// Finds the first element in the collection that satisfies the predicate /// </summary> /// <typeparam name="T">Type of the collection</typeparam> /// <param name="collection">Collection to search</param> /// <param name="predicate">Predicate to satisfy</param> /// <returns>The first element to satisfy the predicate</returns> /// <remarks>If the predicate can not be satisfied default(T) is returned</remarks> public static T Find<T>(this IEnumerable<T> collection, Predicate<T> predicate) { return collection.FirstOrDefault(t => predicate(t)); } }