EnumerableExShareTSource Method (IEnumerableTSource) |
Creates a buffer with a shared view over the source sequence, causing each enumerator to fetch the next element from the source sequence.
Namespace: System.LinqAssembly: System.Interactive (in System.Interactive.dll) Version: 1.2.0.0
Syntaxpublic static IBuffer<TSource> Share<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 elements from the shared source sequence.
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).Share();
var e1 = rng.GetEnumerator(); // Both e1 and e2 will consume elements from
var e2 = rng.GetEnumerator(); // the source sequence.
Assert.IsTrue(e1.MoveNext());
Assert.AreEqual(0, e1.Current);
Assert.IsTrue(e1.MoveNext());
Assert.AreEqual(1, e1.Current);
Assert.IsTrue(e2.MoveNext()); // e2 "steals" element 2
Assert.AreEqual(2, e2.Current);
Assert.IsTrue(e1.MoveNext()); // e1 can't see element 2
Assert.AreEqual(3, e1.Current);
See Also