Method Index
| Edit this page View SourceIndex<TSource>(IEnumerable<TSource>)
Returns a sequence of tuples where the index
is the zero-based index of the item
in the source
sequence.
Declaration
public static IEnumerable<(int Index, TSource Item)> Index<TSource>(this IEnumerable<TSource> source)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TSource> | source | The source sequence. |
Returns
Type | Description |
---|---|
IEnumerable<(int Index, TSource Item)> | A sequence of tuples. |
Type Parameters
Name | Description |
---|---|
TSource | Type of elements in |
Remarks
This operator uses deferred execution and streams its results.
Examples
The following code example demonstrates how to return an index with each element of a sequence using Index
.
var sequence = new[]
{
(key: "jan", value: 123),
(key: "Jan", value: 456),
(key: "JAN", value: 789),
};
// Index a sequence
var result = sequence
.Index();
Console.WriteLine(
"[ " +
string.Join(", ", result) +
" ]");
// This code produces the following output:
// [ (0, (jan, 123)), (1, (Jan, 456)), (2, (JAN, 789)) ]
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
Index<TSource>(IEnumerable<TSource>, int)
Returns a sequence of tuples where the index
is the startIndex
-based index of the
item
in the source
sequence.
Declaration
public static IEnumerable<(int Index, TSource Item)> Index<TSource>(this IEnumerable<TSource> source, int startIndex)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TSource> | source | The source sequence. |
int | startIndex | The index of the first value returned. |
Returns
Type | Description |
---|---|
IEnumerable<(int Index, TSource Item)> | A sequence of tuples. |
Type Parameters
Name | Description |
---|---|
TSource | Type of elements in |
Remarks
This operator uses deferred execution and streams its results.
Examples
The following code example demonstrates how to return an index with each element of a sequence using Index
.
var sequence = new[]
{
(key: "jan", value: 123),
(key: "Jan", value: 456),
(key: "JAN", value: 789),
};
// Index a sequence
var result = sequence
.Index(5);
Console.WriteLine(
"[ " +
string.Join(", ", result) +
" ]");
// This code produces the following output:
// [ (5, (jan, 123)), (6, (Jan, 456)), (7, (JAN, 789)) ]
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|