C# Enumerable Aggregate(IEnumerable, TAccumulate, Func, Func)
Description
Enumerable Aggregate(IEnumerable, TAccumulate, Func, Func)
Applies
an accumulator function over a sequence. The specified seed value is used
as the initial accumulator value, and the specified function is used to select
the result value.
Syntax
Enumerable.Aggregate(IEnumerable, TAccumulate, Func,
Func)
has the following syntax.
public static TResult Aggregate<TSource, TAccumulate, TResult>(
this IEnumerable<TSource> source,
TAccumulate seed,/* w w w . j a v a2 s .c om*/
Func<TAccumulate, TSource, TAccumulate> func,
Func<TAccumulate, TResult> resultSelector
)
Parameters
Enumerable.Aggregate(IEnumerable, TAccumulate, Func, Func)
has the following parameters.
TSource
- The type of the elements of source.TAccumulate
- The type of the accumulator value.TResult
- The type of the resulting value.source
- An IEnumerableto aggregate over. seed
- The initial accumulator value.func
- An accumulator function to be invoked on each element.resultSelector
- A function to transform the final accumulator value into the result value.
Returns
returns TResult
Example
The following code example demonstrates how to use Aggregate to apply an accumulator function and a result selector.
/* www . j ava2 s .c om*/
using System;
using System.Linq;
using System.Collections.Generic;
public class MainClass{
public static void Main(String[] argv){
string[] fruits = { "Java", "C#", "XML", "HTML", "Javascript" };
// Determine whether any string in the array is longer than "XML".
string longestName = fruits.Aggregate("XML",(longest, next) =>
next.Length > longest.Length ? next : longest,
fruit => fruit.ToUpper());
Console.WriteLine("The fruit with the longest name is {0}.",longestName);
}
}