CachedEnumerable Class
Provides factory methods for creating cached enumerables that store enumeration results to prevent multiple enumerations.
public static class CachedEnumerableExample
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
| Name | Description |
|---|---|
| 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.csCreates a cached async enumerable that stores enumeration results.
public static ICachedAsyncEnumerable<T> Create<T>(IAsyncEnumerable<T> enumerable, bool threadSafe = true)Type parameters
T
Parameters
enumerableIAsyncEnumerable<T>threadSafebool- Whether the cached async enumerable should be thread-safe.
Returns
Create<T>(IEnumerable<T>, bool)
CachedEnumerable.csCreates a cached enumerable that stores enumeration results.
public static ICachedEnumerable<T> Create<T>(IEnumerable<T> enumerable, bool threadSafe = true)Type parameters
T
Parameters
enumerableIEnumerable<T>threadSafebool- Whether the cached enumerable should be thread-safe.