Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท LINQ

LINQ Aggregate() method

Reference for LINQ Aggregate() in C#: seed values, accumulator functions, result selectors, empty-sequence errors and when Sum or string.Join is better.

Aggregate() folds a sequence into a single value. It starts with an accumulator, calls your function with the accumulator and each element in turn, and stores the return value as the new accumulator. The final accumulator is the result. Functional languages call this a fold or reduce.

It is the general tool behind Sum(), Count() and string.Join(). Use it for custom combinations such as running products, building a string or dictionary, or merging objects. For common cases a dedicated method is shorter and often faster.

C#
int product = numbers.Aggregate((acc, n) => acc * n);
string csv = names.Aggregate("", (acc, n) => acc + n + ";");

Try it

C# Seeds and accumulators

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# Building collections and empty sequences

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Syntax

C#
public static TSource Aggregate<TSource>(
    this IEnumerable<TSource> source, Func<TSource, TSource, TSource> func);
public static TAccumulate Aggregate<TSource, TAccumulate>(
    this IEnumerable<TSource> source, TAccumulate seed,
    Func<TAccumulate, TSource, TAccumulate> func);
public static TResult Aggregate<TSource, TAccumulate, TResult>(
    this IEnumerable<TSource> source, TAccumulate seed,
    Func<TAccumulate, TSource, TAccumulate> func,
    Func<TAccumulate, TResult> resultSelector);
Name Description
seed The initial accumulator value.
func Combines the current accumulator with the next element and returns the new accumulator.
resultSelector Converts the final accumulator into the returned value.

Returns: the final accumulator (or the selected result). It runs immediately; it is not deferred.

Errors: ArgumentNullException for a null source or delegate. InvalidOperationException if you use the seedless overload on an empty sequence.

Usage notes

  • Seedless vs seeded: without a seed the accumulator type equals the element type and the first element is the starting value (the function is called n - 1 times). With a seed it is called n times and the sequence may be empty.
  • Order matters for non-commutative functions such as subtraction or string concatenation: elements are processed from first to last.
  • Prefer dedicated operators: Sum(), Max(), Count(), string.Join() and MaxBy() are clearer, and string.Join() avoids creating many intermediate strings.
  • Mutating the seed: if the accumulator is a mutable object (a list or dictionary), the same object is modified and returned. That is efficient, but do not reuse the seed instance across several calls.
  • String concatenation in a fold is O(n squared). Use a StringBuilder as the seed for large inputs.
  • No parallel safety: Aggregate() is sequential. PLINQ has its own overload with combine functions.
  • Integer overflow: arithmetic inside the function is unchecked by default; wrap the code in checked if overflow should throw.