Mega Code Archive

 
Categories / C# / Collections Data Structure
 

Returns true if there are enough items in the IEnumerableT

using System.Linq; using System.Diagnostics; namespace System.Collections.Generic {     public static class EnumeratorExtensions     {         /// <summary>         /// Returns true if there are enough items in the IEnumerable&lt;T&gt;.         /// </summary>         /// <typeparam name="T"></typeparam>         /// <param name="source">The source list of items.</param>         /// <param name="number">The number of items to check for.</param>         /// <returns>True if there are enough items, false otherwise.</returns>         public static bool AtLeast<T>(this IEnumerable<T> source, int number)         {             if (source == null)                 throw new ArgumentNullException("source");             int count = 0;             using (IEnumerator<T> data = source.GetEnumerator())                 while (count < number && data.MoveNext())                 {                     count++;                 }             return count == number;         }         /// <summary>         /// Returns true if there are enough items in the IEnumerable&lt;T&gt; to satisfy the condition.         /// </summary>         /// <typeparam name="T"></typeparam>         /// <param name="source">The source list of items.</param>         /// <param name="number">The number of items to check for.</param>         /// <param name="predicate">The condition to apply to the items.</param>         /// <returns>True if there are enough items, false otherwise.</returns>         public static bool AtLeast<T>(this IEnumerable<T> source, int number, Func<T, bool> condition)         {             if (source == null)                 throw new ArgumentNullException("source");             if (condition == null)                 throw new ArgumentNullException("condition");             int count = 0;             using (IEnumerator<T> data = source.GetEnumerator())                 while (count < number && data.MoveNext())                 {                     if (condition(data.Current))                         count++;                 }             return count == number;         }     } }