Method Interleave
| Edit this page View SourceInterleave<T>(IEnumerable<T>, params IEnumerable<T>[])
Interleaves the elements of two or more sequences into a single sequence in a round-robin fashion until all sequences are consumed.
Declaration
public static IEnumerable<T> Interleave<T>(this IEnumerable<T> source, params IEnumerable<T>[] otherSources)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | source | The first sequence in the interleave group |
IEnumerable<T>[] | otherSources | The other sequences in the interleave group |
Returns
Type | Description |
---|---|
IEnumerable<T> | A sequence of interleaved elements from all of the source sequences |
Type Parameters
Name | Description |
---|---|
T | The type of the elements of the source sequences |
Remarks
This method uses deferred execution and streams its results.
Examples
The following code example demonstrates how to interleave the elements from multiple sequences into a single sequence using Interleave
.
var seq1 = Enumerable.Range(1, 5);
var seq2 = Enumerable.Range(1, 4);
var seq3 = Enumerable.Range(1, 3);
// Interleave the elements from multiple sequences into a single sequence
var result = seq1
.Interleave(seq2, seq3);
Console.WriteLine(string.Join(", ", result));
// This code produces the following output:
// 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 5
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
Interleave<T>(IEnumerable<IEnumerable<T>>)
Interleaves the elements of two or more sequences into a single sequence in a round-robin fashion until all sequences are consumed.
Declaration
public static IEnumerable<T> Interleave<T>(this IEnumerable<IEnumerable<T>> sources)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<IEnumerable<T>> | sources | The sequences to interleave together |
Returns
Type | Description |
---|---|
IEnumerable<T> | A sequence of interleaved elements from all of the source sequences |
Type Parameters
Name | Description |
---|---|
T | The type of the elements of the source sequences |
Remarks
This method uses deferred execution and streams its results.
Examples
The following code example demonstrates how to interleave the elements from multiple sequences into a single sequence using Interleave
.
var seq1 = Enumerable.Range(1, 5);
var seq2 = Enumerable.Range(1, 4);
var seq3 = Enumerable.Range(1, 3);
// Interleave the elements from multiple sequences into a single sequence
var result = new[] { seq1, seq2, seq3, }
.Interleave();
Console.WriteLine(string.Join(", ", result));
// This code produces the following output:
// 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 5
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|