Method AggregateRight
| Edit this page View SourceAggregateRight<TSource>(IEnumerable<TSource>, Func<TSource, TSource, TSource>)
Applies a right-associative accumulator function over a sequence. This operator is the right-associative version of the Aggregate<TSource>(IEnumerable<TSource>, Func<TSource, TSource, TSource>) LINQ operator.
Declaration
public static TSource AggregateRight<TSource>(this IEnumerable<TSource> source, Func<TSource, TSource, TSource> func)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TSource> | source | Source sequence. |
Func<TSource, TSource, TSource> | func | A right-associative accumulator function to be invoked on each element. |
Returns
Type | Description |
---|---|
TSource | The final accumulator value. |
Type Parameters
Name | Description |
---|---|
TSource | The type of the elements of source. |
Remarks
This operator executes immediately.
Examples
The following code example demonstrates how to collect numbers into a string using AggregateRight
.
var numbers = new string[] { "1", "2", "3", "4", "5", };
// Enumerate strings from right to left and collect the text into larger strings.
var result = numbers
.AggregateRight((a, b) => $"({a}/{b})");
Console.WriteLine(result);
// This code produces the following output:
// (1/(2/(3/(4/5))))
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
AggregateRight<TSource, TAccumulate>(IEnumerable<TSource>, TAccumulate, Func<TSource, TAccumulate, TAccumulate>)
Applies a right-associative accumulator function over a sequence. The specified seed
value is used as the initial accumulator value. This operator is the right-associative version of the Aggregate<TSource, TAccumulate>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate, TSource, TAccumulate>) LINQ operator.
Declaration
public static TAccumulate AggregateRight<TSource, TAccumulate>(this IEnumerable<TSource> source, TAccumulate seed, Func<TSource, TAccumulate, TAccumulate> func)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TSource> | source | Source sequence. |
TAccumulate | seed | The initial accumulator value. |
Func<TSource, TAccumulate, TAccumulate> | func | A right-associative accumulator function to be invoked on each element. |
Returns
Type | Description |
---|---|
TAccumulate | The final accumulator value. |
Type Parameters
Name | Description |
---|---|
TSource | The type of the elements of source. |
TAccumulate | The type of the accumulator value. |
Remarks
This operator executes immediately.
Examples
The following code example demonstrates how to collect numbers into a string using AggregateRight
.
var numbers = new string[] { "1", "2", "3", "4", "5", };
// Enumerate strings from right to left and collect the text into larger strings.
var result = numbers
.AggregateRight("6", (a, b) => $"({a}/{b})");
Console.WriteLine(result);
// This code produces the following output:
// (1/(2/(3/(4/(5/6)))))
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
AggregateRight<TSource, TAccumulate, TResult>(IEnumerable<TSource>, TAccumulate, Func<TSource, TAccumulate, TAccumulate>, Func<TAccumulate, TResult>)
Applies a right-associative accumulator function over a sequence. The specified seed
value is used as the initial accumulator value, and the resultSelector
function is used
to select the result value. This operator is the right-associative version of the Aggregate<TSource, TAccumulate, TResult>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate, TSource, TAccumulate>, Func<TAccumulate, TResult>) LINQ operator.
Declaration
public static TResult AggregateRight<TSource, TAccumulate, TResult>(this IEnumerable<TSource> source, TAccumulate seed, Func<TSource, TAccumulate, TAccumulate> func, Func<TAccumulate, TResult> resultSelector)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TSource> | source | Source sequence. |
TAccumulate | seed | The initial accumulator value. |
Func<TSource, TAccumulate, TAccumulate> | func | A right-associative accumulator function to be invoked on each element. |
Func<TAccumulate, TResult> | resultSelector | A function to transform the final accumulator value into the result value. |
Returns
Type | Description |
---|---|
TResult | The transformed final accumulator value. |
Type Parameters
Name | Description |
---|---|
TSource | The type of the elements of source. |
TAccumulate | The type of the accumulator value. |
TResult | The type of the resulting value. |
Remarks
This operator executes immediately.
Examples
The following code example demonstrates how to collect numbers into a string using AggregateRight
.
var numbers = new string[] { "1", "2", "3", "4", "5", };
// Enumerate strings from right to left and collect the text into larger strings.
var result = numbers
.AggregateRight(
"6",
(a, b) => $"({a}/{b})",
s => s.Length);
Console.WriteLine(result);
// This code produces the following output:
// 21
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|