Method TagFirstLast
| Edit this page View SourceTagFirstLast<TSource>(IEnumerable<TSource>)
Returns a sequence of tuples containing the element and flags indicating whether the element is the first and/or last of the sequence.
Declaration
public static IEnumerable<(TSource item, bool isFirst, bool isLast)> TagFirstLast<TSource>(this IEnumerable<TSource> source)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TSource> | source | The source sequence. |
Returns
Type | Description |
---|---|
IEnumerable<(TSource item, bool isFirst, bool isLast)> | Returns the resulting sequence. |
Type Parameters
Name | Description |
---|---|
TSource | The type of the elements of |
Remarks
This operator uses deferred execution and streams its results.
Examples
The following code example demonstrates how tag elements of a sequence with information on whether it is the first or last element of the sequence using TagFirstLast
.
var sequence = Enumerable.Range(1, 4);
// Replace a value in a sequence
var result = sequence
.TagFirstLast();
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [(1, True, False), (2, False, False), (3, False, False), (4, False, True)]
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
TagFirstLast<TSource, TResult>(IEnumerable<TSource>, Func<TSource, bool, bool, TResult>)
Returns a sequence resulting from applying a function to each element in the source sequence with additional parameters indicating whether the element is the first and/or last of the sequence.
Declaration
public static IEnumerable<TResult> TagFirstLast<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, bool, bool, TResult> resultSelector)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TSource> | source | The source sequence. |
Func<TSource, bool, bool, TResult> | resultSelector | A function that determines how to project the each element along with its first or last tag. |
Returns
Type | Description |
---|---|
IEnumerable<TResult> | Returns the resulting sequence. |
Type Parameters
Name | Description |
---|---|
TSource | The type of the elements of |
TResult | The type of the element of the returned sequence. |
Remarks
This operator uses deferred execution and streams its results.
Examples
The following code example demonstrates how tag elements of a sequence with information on whether it is the first or last element of the sequence using TagFirstLast
.
var sequence = Enumerable.Range(1, 4);
// Replace a value in a sequence
var result = sequence
.TagFirstLast(
(item, first, last) => new
{
Item = item,
IsEdge = first || last,
});
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [{ Item = 1, IsEdge = True }, { Item = 2, IsEdge = False }, { Item = 3, IsEdge = False }, { Item = 4, IsEdge = True }]
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|