SuperLinq SuperLinq
SuperLinq SuperLinq
DocFX + Singulink = ♥

Search Results for

    Method TakeEvery

    | Edit this page

    TakeEvery<TSource>(IEnumerable<TSource>, int)

    Returns every N-th element of a sequence.

    Declaration
    public static IEnumerable<TSource> TakeEvery<TSource>(this IEnumerable<TSource> source, int step)
    Parameters
    Type Name Description
    IEnumerable<TSource> source

    Source sequence

    int step

    Number of elements to bypass before returning the next element.

    Returns
    Type Description
    IEnumerable<TSource>

    A sequence with every N-th element of the input sequence.

    Type Parameters
    Name Description
    TSource

    Type of the source sequence

    Remarks

    This operator uses deferred execution and streams its results.

    Examples

    The following code example demonstrates how to take every n-th element of a sequence using TakeEvery.

    var sequence = Enumerable.Range(1, 6);
    
    // Take every 2nd element of the sequence
    var result = sequence
        .TakeEvery(2);
    
    Console.WriteLine(
        "[" +
        string.Join(", ", result) +
        "]");
    
    // This code produces the following output:
    // [1, 3, 5]
    
    Exceptions
    Type Condition
    ArgumentNullException

    source is null.

    ArgumentOutOfRangeException

    step is negative.

    © SuperLinq Authors. All rights reserved.