Returns true if any item in the target collection satisfies the specified predicate. - CSharp System.Collections.Generic

CSharp examples for System.Collections.Generic:ICollection

Description

Returns true if any item in the target collection satisfies the specified predicate.

Demo Code

// All rights reserved.
using System.Collections.Generic;
using System.Collections;
using System;/*from  ww  w. ja va2s  . co  m*/

public class Main{
        /// <summary>
      /// Returns true if any item in the target collection satisfies the specified predicate.
      /// </summary>
      public static bool Contains(IEnumerable target, Predicate<object> predicate)
      {
         foreach (var item in target)
         {
            if (predicate(item))
               return true;
         }
         return false;
      }
        /// <summary>
      /// Returns true if any item in the target collection satisfies the specified predicate.
      /// </summary>
      public static bool Contains<TItem>(IEnumerable<TItem> target, Predicate<TItem> predicate)
      {
         return Contains((IEnumerable)target, predicate);
      }
        /// <summary>
      /// Returns true if any item in the target collection satisfies the specified predicate.
      /// </summary>
      public static bool Contains<TItem>(IEnumerable target, Predicate<TItem> predicate)
      {
         foreach (TItem item in target)
         {
            if (predicate(item))
               return true;
         }
         return false;
      }
}

Related Tutorials