EnumerableExMemoizeTSource Method (IEnumerableTSource) |
Creates a buffer with a view over the source sequence, causing each enumerator to obtain access to all of the sequence's elements without causing multiple enumerations over the source.
Namespace: System.LinqAssembly: System.Interactive (in System.Interactive.dll) Version: 1.2.0.0
Syntaxpublic static IBuffer<TSource> Memoize<TSource>(
this IEnumerable<TSource> source
)
Parameters
- source
- Type: System.Collections.GenericIEnumerableTSource
Source sequence.
Type Parameters
- TSource
- Source sequence element type.
Return Value
Type:
IBufferTSourceBuffer enabling each enumerator to retrieve all elements from the shared source sequence, without duplicating source enumeration side-effects.
Usage Note
In Visual Basic and C#, you can call this method as an instance method on any object of type
IEnumerableTSource. When you use instance method syntax to call this method, omit the first parameter. For more information, see
Extension Methods (Visual Basic) or
Extension Methods (C# Programming Guide).
Examples
var rng = Enumerable.Range(0, 10).Do(x => Console.WriteLine(x)).Memoize();
var e1 = rng.GetEnumerator();
Assert.IsTrue(e1.MoveNext()); // Prints 0
Assert.AreEqual(0, e1.Current);
Assert.IsTrue(e1.MoveNext()); // Prints 1
Assert.AreEqual(1, e1.Current);
var e2 = rng.GetEnumerator();
Assert.IsTrue(e2.MoveNext()); // Doesn't print anything; the side-effect of Do
Assert.AreEqual(0, e2.Current); // has already taken place during e1's iteration.
Assert.IsTrue(e1.MoveNext()); // Prints 2
Assert.AreEqual(2, e1.Current);
See Also