InterleaveExtensionInterleaveT Method

Interleaves the elements of two or more sequences into a single sequence, skipping sequences as they are consumed.

Definition

Namespace: MoreLinq.Extensions
Assembly: MoreLinq (in MoreLinq.dll) Version: 3.4.0+b99a6a8cc504caf2d48372fe54a2f8116c59cd0c
C#
public static IEnumerable<T> Interleave<T>(
	this IEnumerable<T> sequence,
	params IEnumerable<T>[] otherSequences
)

Parameters

sequence  IEnumerableT
The first sequence in the interleave group.
otherSequences  IEnumerableT
The other sequences in the interleave group.

Type Parameters

T
The type of the elements of the source sequences.

Return Value

IEnumerableT
A sequence of interleaved elements from all of the source sequences.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type IEnumerableT. 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).

Remarks

Interleave combines sequences by visiting each in turn, and returning the first element of each, followed by the second, then the third, and so on. So, for example:

C#
var xs = new[] { 1, 1, 1 }.Interleave(new[] { 2, 2, 2 }, new[] { 3, 3, 3 });
// xs = { 1, 2, 3, 1, 2, 3, 1, 2, 3 }

This operator behaves in a deferred and streaming manner.

When sequences are of unequal length, this method will skip those sequences that have been fully consumed and continue interleaving the remaining sequences.

The sequences are interleaved in the order that they appear in the otherSequences collection, with sequence as the first sequence.

See Also