Method Insert
| Edit this page View SourceInsert<T>(IEnumerable<T>, IEnumerable<T>, int)
Inserts the elements of a sequence into another sequence at a specified index.
Declaration
public static IEnumerable<T> Insert<T>(this IEnumerable<T> first, IEnumerable<T> second, int index)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | first | The source sequence. |
IEnumerable<T> | second | The sequence that will be inserted. |
int | index | The zero-based index at which to insert elements from |
Returns
Type | Description |
---|---|
IEnumerable<T> | A sequence that contains the elements of |
Type Parameters
Name | Description |
---|---|
T | Type of the elements of the source sequence. |
Remarks
This method uses deferred execution and streams its results.
Examples
The following code example demonstrates how to insert one sequence into the middle of another sequence using Insert
.
var first = Enumerable.Range(1, 5);
var second = Enumerable.Range(1, 5);
// Insert one sequence into another
var result = first
.Insert(second, 2);
Console.WriteLine(string.Join(", ", result));
// This code produces the following output:
// 1, 2, 1, 2, 3, 4, 5, 3, 4, 5
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ArgumentOutOfRangeException | Thrown if |
ArgumentOutOfRangeException | Thrown lazily if |
Insert<T>(IEnumerable<T>, IEnumerable<T>, Index)
Inserts the elements of a sequence into another sequence at a specified index.
Declaration
public static IEnumerable<T> Insert<T>(this IEnumerable<T> first, IEnumerable<T> second, Index index)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | first | The source sequence. |
IEnumerable<T> | second | The sequence that will be inserted. |
Index | index | The zero-based index at which to insert elements from |
Returns
Type | Description |
---|---|
IEnumerable<T> | A sequence that contains the elements of |
Type Parameters
Name | Description |
---|---|
T | Type of the elements of the source sequence. |
Remarks
This method uses deferred execution and streams its results.
Examples
The following code example demonstrates how to insert one sequence into the middle of another sequence using Insert
.
var first = Enumerable.Range(1, 5);
var second = Enumerable.Range(1, 5);
// Insert one sequence into another
var result = first
.Insert(second, ^2);
Console.WriteLine(string.Join(", ", result));
// This code produces the following output:
// 1, 2, 3, 1, 2, 3, 4, 5, 4, 5
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ArgumentOutOfRangeException | Thrown if |
ArgumentOutOfRangeException | Thrown lazily if |