Exists in IEnumerable by Predicate - CSharp System.Collections

CSharp examples for System.Collections:IEnumerable

Description

Exists in IEnumerable by Predicate

Demo Code


using System.Collections.Generic;
using System.Collections;
using System;/*w  ww  .  j a  va2s.  co m*/

public class Main{

        internal static bool Exists<T>(IEnumerable<T> collection, Predicate<T> predicate)
        {
            if (collection == null)
                throw new ArgumentNullException("collection");
            if (predicate == null)
                throw new ArgumentNullException("predicate");

            foreach (T item in collection) {
                if (predicate(item))
                    return true;
            }

            return false;
        }
}

Related Tutorials