.NET API Documentation

System.Linq.Enumerable.Aggregate

Summary

Applies an accumulator function over a sequence. The current method supports three overloads:

  • Applies an accumulator function over a sequence.
  • Applies an accumulator function over a sequence. The specified seed value is used as the initial accumulator value.
  • Applies an accumulator function over a sequence. The specified seed value is used as the initial accumulator value, and the specified selector function is used to select the result.

Syntax

public static TResult Aggregate<TSource, TAccumulate, TResult>(IEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> func, Func<TAccumulate, TResult> selector)
public static TAccumulate Aggregate<TSource, TAccumulate>(IEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> func)
public static TSource Aggregate<TSource>(IEnumerable<TSource> source, Func<TSource, TSource, TSource> func)

Parameters

  • source: IEnumerable<TSource>
    An IEnumerable<TSource> to aggregate over.
  • seed: TAccumulate
    The initial accumulator value.
  • func: Func<TAccumulate, TSource, TAccumulate> or Func<TSource, TSource, TSource>
    An accumulator function to apply over each source element; the second parameter of the function to be executed is the current element of the sequence.
  • selector: Func<TAccumulate, TResult>
    A transform function to apply to the result.

Returns

  • TResult or TAccumulate or TSource
  • The transformed accumulator value or the result of the final aggregation.

Exceptions

ArgumentNullException: source is null.

ArgumentException: The source sequence is empty and no seed is provided. (For the overload without a seed).

Examples

Example 1: Simple Summation

Calculate the sum of a list of numbers.

using System.Linq;

int[] numbers = { 1, 2, 3, 4, 5 };

// Using the overload with a function
int sum = numbers.Aggregate(new Func<int, int, int>((acc, n) => acc + n));

// Output: 15
Console.WriteLine(sum);

Example 2: Concatenating Strings

Concatenate strings in a list with a separator.

using System.Linq;

string[] words = { "Aggregate", "is", "powerful" };

// Using the overload with a seed and a function
string sentence = words.Aggregate("Result: ", new Func<string, string, string>((acc, word) => acc + " " + word));

// Output: Result: Aggregate is powerful
Console.WriteLine(sentence);

Example 3: Complex Aggregation with Selector

Find the length of the longest word, transformed into an uppercase string.

using System.Linq;

string[] words = { "apple", "banana", "cherry", "date" };

// Using the overload with seed, function, and selector
string longestWordUppercase = words.Aggregate(
    "", // Initial seed
    (new Func<string, string, string>((longest, next) =>
        next.Length > longest.Length ? next : longest)), // Accumulator function
    (new Func<string, string>(longest => longest.ToUpper())) // Selector function
);

// Output: BANANA
Console.WriteLine(longestWordUppercase);