Skip to content

Add HasAtLeast<TSource>(this IEnumerable<TSource>, int) #19505

@shmuelie

Description

@shmuelie

Motivation

When working with sequences of just IEnumerable<T> calling Count() can be very expensive if Count() is forced to enumerate through the whole sequence. When checking if a sequence has X elements or more calling Count() is not just expensive but wasteful.

Proposal

namespace System.Linq
{
    public static class Enumerable
    {
+       /// <summary>
+       ///     Returns if a sequence has at least a certain amount of elements.
+       /// </summary>
+       /// <typeparam name="TSource">The type of elements of <paramref name="source"/>.</typeparam>
+       /// <param name="source">An <see cref="IEnumerable{T}"/> to check the length of.</param>
+       /// <param name="atLeast">The minimum amount of items that <paramref name="source"/> should have.</param>
+       /// <returns><see langword="true"/> if <paramref name="source"/> has at least <paramref name="atLeast"/> elements. <see langword="false"/> otherwise.</returns>
+       /// <exception cref="ArgumentNullException"><paramref name="source"/> is <see langword="null"/>.</exception>
+       /// <exception cref="ArgumentOutOfRangeException"><paramref name="atLeast"/> must be zero or greater.</exception>
+       public static bool HasAtLeast<TSource>(this IEnumerable<TSource> source, int atLeast);
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-needs-workAPI needs work before it is approved, it is NOT ready for implementationarea-System.Linq

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions