SuperLinq SuperLinq
SuperLinq SuperLinq
DocFX + Singulink = ♥

Search Results for

    Method Share

    | Edit this page

    Share<TSource>(IEnumerable<TSource>)

    Creates a buffer with a shared view over the source sequence, causing each enumerator to fetch the next element from the source sequence.

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

    Source sequence.

    Returns
    Type Description
    IBuffer<TSource>

    Buffer enabling each enumerator to retrieve elements from the shared source sequence.

    Type Parameters
    Name Description
    TSource

    Source sequence element type.

    Remarks

    This operator uses deferred execution and streams its result.

    Examples

    The following code example demonstrates how to share a single view of an enumerator across multiple consumers using Share.

    var sequence = Enumerable.Range(0, 10);
    
    // allow multiple consumers to cache views of the same sequence
    using var rng = sequence.Share();
    using var e1 = rng.GetEnumerator();    // e1 has a shared view on the source
    
    Debug.Assert(e1.MoveNext());
    Console.WriteLine("e1.MoveNext()");
    Console.WriteLine($"e1.Current: {e1.Current}");
    
    Debug.Assert(e1.MoveNext());
    Console.WriteLine("e1.MoveNext()");
    Console.WriteLine($"e1.Current: {e1.Current}");
    
    using var e2 = rng.GetEnumerator();    // e2 has a shared view on the source
    
    Debug.Assert(e2.MoveNext());    // e2 enumerates over the shared view, advancing the source
    Console.WriteLine("e2.MoveNext()");
    Console.WriteLine($"e1.Current: {e1.Current}");
    Console.WriteLine($"e2.Current: {e2.Current}");
    
    Debug.Assert(e1.MoveNext());    // e1 enumerates over the shared view, advancing the source
    Console.WriteLine("e1.MoveNext()");
    Console.WriteLine($"e1.Current: {e1.Current}");
    Console.WriteLine($"e2.Current: {e2.Current}");
    
    // This code produces the following output:
    // e1.MoveNext()
    // e1.Current: 0
    // e1.MoveNext()
    // e1.Current: 1
    // e2.MoveNext()
    // e1.Current: 1
    // e2.Current: 2
    // e1.MoveNext()
    // e1.Current: 3
    // e2.Current: 2
    
    Exceptions
    Type Condition
    ArgumentNullException

    source is null

    © SuperLinq Authors. All rights reserved.