Language: C#
Collection Extensions
/// <summary> /// Find an object in a value-type collection. /// </summary> /// <typeparam name="T">Type of collection.</typeparam> /// <param name="collection">The collection to search.</param> /// <param name="isMatch">The Predicate to test to find the collection item.</param> /// <returns>The collection item.</returns> public static T GetValueItem<T>(this ICollection<T> collection, Predicate<T> isMatch) { foreach (var item in collection) { if (isMatch(item)) return item; } return default(T); } /// <summary> /// Find an object in a reference type collection. /// </summary> /// <typeparam name="T">Type of collection.</typeparam> /// <param name="collection">The collection to search.</param> /// <param name="isMatch">The Predicate to test to find the collection item.</param> /// <returns>The collection item.</returns> public static T GetReferenceItem<T>(this ICollection<T> collection, Predicate<T> isMatch) where T : class, new() { foreach (var item in collection) { if (isMatch(item)) return item; } return new T(); }
Tags:
Description:
Wrote this to extend Value type and Reference Type collections to locate an item in testing
Report Abuse
Subscribe
Discuss
What's new
What is it
New Snippet
Recent Snippets
My Snippets
Web Code
Search

