CachedEnumerable Class

Provides factory methods for creating cached enumerables that store enumeration results to prevent multiple enumerations.

public static class CachedEnumerable

Example

var enumerationCount = 0;
IEnumerable<int> GetExpensiveEnumerable()
{
    enumerationCount++;
    yield return 1;
    yield return 2;
}

using var cached = CachedEnumerable.Create(GetExpensiveEnumerable());
Assert.Equal([1, 2], cached); // First enumeration
Assert.Equal([1, 2], cached); // Uses cached results
Assert.Equal(1, enumerationCount);

Methods

NameDescription
Create<T>(IAsyncEnumerable<T>, bool)Creates a cached async enumerable that stores enumeration results.
Create<T>(IEnumerable<T>, bool)Creates a cached enumerable that stores enumeration results.

Create overloads

Create<T>(IAsyncEnumerable<T>, bool)

CachedEnumerable.cs

Creates a cached async enumerable that stores enumeration results.

public static ICachedAsyncEnumerable<T> Create<T>(IAsyncEnumerable<T> enumerable, bool threadSafe = true)
Type parameters
T
Parameters
enumerable IAsyncEnumerable<T>
threadSafe bool
Whether the cached async enumerable should be thread-safe.
Returns

ICachedAsyncEnumerable<T>

Create<T>(IEnumerable<T>, bool)

CachedEnumerable.cs

Creates a cached enumerable that stores enumeration results.

public static ICachedEnumerable<T> Create<T>(IEnumerable<T> enumerable, bool threadSafe = true)
Type parameters
T
Parameters
enumerable IEnumerable<T>
threadSafe bool
Whether the cached enumerable should be thread-safe.
Returns

ICachedEnumerable<T>