Method CountDown
| Edit this page View SourceCountDown<TSource>(IEnumerable<TSource>, int)
Provides a countdown counter for a given count of elements at the tail of the sequence where zero always represents the last element, one represents the second-last element, two represents the third-last element and so on.
Declaration
public static IEnumerable<(TSource item, int? count)> CountDown<TSource>(this IEnumerable<TSource> source, int count)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TSource> | source | The source sequence. |
int | count | Count of tail elements of |
Returns
Type | Description |
---|---|
IEnumerable<(TSource item, int? count)> | A sequence of tuples containing the element and it's count from the end of the sequence, or null. |
Type Parameters
Name | Description |
---|---|
TSource | The type of elements of |
Remarks
At most, count
elements of the source sequence may be buffered at any one time unless
source
is a collection or a list.
This method uses deferred execution semantics and streams its results.
Examples
The following code example demonstrates how to get a count down timer for a sequence, using the CountDown
operator:
var sequence = Enumerable.Range(1, 5);
// Get a countdown counter for the the sequence
var result = sequence.CountDown(2);
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [(1, ), (2, ), (3, ), (4, 1), (5, 0)]
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ArgumentOutOfRangeException |
|
CountDown<TSource, TResult>(IEnumerable<TSource>, int, Func<TSource, int?, TResult>)
Provides a countdown counter for a given count of elements at the tail of the sequence where zero always represents the last element, one represents the second-last element, two represents the third-last element and so on.
Declaration
public static IEnumerable<TResult> CountDown<TSource, TResult>(this IEnumerable<TSource> source, int count, Func<TSource, int?, TResult> resultSelector)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TSource> | source | The source sequence. |
int | count | Count of tail elements of |
Func<TSource, int?, TResult> | resultSelector | A function that receives the element and the current countdown value for the element and which returns those
mapped to a result returned in the resulting sequence. For elements before the last |
Returns
Type | Description |
---|---|
IEnumerable<TResult> | A sequence of results returned by |
Type Parameters
Name | Description |
---|---|
TSource | The type of elements of |
TResult | The type of elements of the resulting sequence. |
Remarks
At most, count
elements of the source sequence may be buffered at any one time unless
source
is a collection or a list.
This method uses deferred execution semantics and streams its results.
Examples
The following code example demonstrates how to get a count down timer for a sequence, using the CountDown
operator:
var sequence = Enumerable.Range(1, 5);
// Get a countdown counter for the the sequence
var result = sequence.CountDown(2, (item, cd) => new { Item = item, CountDown = cd?.ToString() ?? "null", });
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [{ Item = 1, CountDown = null }, { Item = 2, CountDown = null }, { Item = 3, CountDown = null }, { Item = 4, CountDown = 1 }, { Item = 5, CountDown = 0 }]
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ArgumentOutOfRangeException |
|