SuperLinq SuperLinq
SuperLinq SuperLinq
DocFX + Singulink = ♥

Search Results for

    Method Retry

    | Edit this page

    Retry<TSource>(IEnumerable<TSource>)

    Creates a sequence that retries enumerating the source sequence as long as an error occurs.

    Declaration
    public static IEnumerable<TSource> Retry<TSource>(this IEnumerable<TSource> source)
    Parameters
    Type Name Description
    IEnumerable<TSource> source

    Source sequence.

    Returns
    Type Description
    IEnumerable<TSource>

    Sequence concatenating the results of the source sequence as long as an error occurs.

    Type Parameters
    Name Description
    TSource

    Source sequence element type.

    Remarks

    source will be enumerated and values streamed until it either completes or encounters an error. If an error is thrown, then source will be re-enumerated from the beginning. This will happen until an iteration of source has completed without errors.

    This method uses deferred execution and streams its results.

    Examples

    The following code example demonstrates how to retry sequences on failure using Retry.

    // this sequence will throw an exception on the 4th element
    var sequence = Enumerable.Range(1, 3)
        .Concat(SuperEnumerable.Throw<int>(new InvalidOperationException()));
    
    // Re-enumerate the sequence on failure indefinitely
    var result = sequence
        .Retry()
        .Take(12);
    
    Console.WriteLine(
        "[" + 
        string.Join(", ", result) +
        "]");
    
    // This code produces the following output:
    // [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]
    
    Exceptions
    Type Condition
    ArgumentNullException

    source is null.

    | Edit this page

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

    Creates a sequence that retries enumerating the source sequence as long as an error occurs, with the specified maximum number of retries.

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

    Source sequence.

    int count

    Maximum number of retries.

    Returns
    Type Description
    IEnumerable<TSource>

    Sequence concatenating the results of the source sequence as long as an error occurs.

    Type Parameters
    Name Description
    TSource

    Source sequence element type.

    Remarks

    source will be enumerated and values streamed until it either completes or encounters an error. If an error is thrown, then source will be re-enumerated from the beginning. This will happen until an iteration of source has completed without errors, or source has been enumerated count times. If an error is thrown during the final iteration, it will not be caught and will be thrown to the consumer.

    This method uses deferred execution and streams its results.

    Examples

    The following code example demonstrates how to retry sequences on failure using Retry.

    // this sequence will throw an exception on the 4th element
    var sequence = Enumerable.Range(1, 3)
        .Concat(SuperEnumerable.Throw<int>(new InvalidOperationException()));
    
    // Re-enumerate the sequence on failure up to n times
    var result = sequence
        .Retry(5)
        .Take(12);
    
    Console.WriteLine(
        "[" + 
        string.Join(", ", result) +
        "]");
    
    // This code produces the following output:
    // [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]
    
    Exceptions
    Type Condition
    ArgumentNullException

    source is null.

    ArgumentOutOfRangeException

    count is less than or equal to 0.

    © SuperLinq Authors. All rights reserved.