Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Aggregate method in C#
The Aggregate method in C# is a powerful LINQ extension method that performs a sequential operation on all elements in a collection. It applies an accumulator function over a sequence, allowing you to perform custom aggregations like mathematical operations, string concatenations, or complex data transformations.
Syntax
Following are the main overloads of the Aggregate method −
// Basic syntax - uses first element as seed
public static TSource Aggregate<TSource>(
this IEnumerable<TSource> source,
Func<TSource, TSource, TSource> func
);
// With seed value
public static TAccumulate Aggregate<TSource, TAccumulate>(
this IEnumerable<TSource> source,
TAccumulate seed,
Func<TAccumulate, TSource, TAccumulate> func
);
Parameters
- source − The sequence to aggregate over
- func − An accumulator function to be invoked on each element
- seed − The initial accumulator value (optional)
Using Aggregate for Mathematical Operations
Example - Multiplication
Here is how to multiply all elements in an array using the Aggregate method −
using System;
using System.Linq;
public class Demo {
public static void Main() {
int[] arr = { 10, 15, 20 };
// Multiplication using Aggregate
int result = arr.Aggregate((x, y) => x * y);
Console.WriteLine("Product: " + result);
// Step-by-step: 10 * 15 = 150, then 150 * 20 = 3000
}
}
The output of the above code is −
Product: 3000
Example - Sum with Seed Value
Using a seed value allows you to start with an initial accumulator −
using System;
using System.Linq;
public class Demo {
public static void Main() {
int[] numbers = { 5, 10, 15 };
// Sum with seed value of 100
int sum = numbers.Aggregate(100, (acc, x) => acc + x);
Console.WriteLine("Sum with seed: " + sum);
// Finding maximum value
int max = numbers.Aggregate((x, y) => x > y ? x : y);
Console.WriteLine("Maximum: " + max);
}
}
The output of the above code is −
Sum with seed: 130 Maximum: 15
Using Aggregate for String Operations
Example - String Concatenation
using System;
using System.Linq;
public class Demo {
public static void Main() {
string[] words = { "Hello", "World", "C#", "Programming" };
// Concatenate with spaces
string sentence = words.Aggregate((x, y) => x + " " + y);
Console.WriteLine("Sentence: " + sentence);
// Create CSV format with seed
string csv = words.Aggregate("Items:", (acc, word) => acc + " " + word + ",");
Console.WriteLine("CSV: " + csv.TrimEnd(','));
}
}
The output of the above code is −
Sentence: Hello World C# Programming CSV: Items: Hello, World, C#, Programming
Common Use Cases
- Mathematical operations − Product, sum, finding min/max values
- String manipulation − Concatenation, building formatted strings
- Custom aggregations − Complex data transformations and calculations
- Functional programming − Reducing collections to single values
Conclusion
The Aggregate method in C# provides a flexible way to perform sequential operations on collections, from simple mathematical calculations to complex data transformations. It's particularly useful when built-in methods like Sum() or Max() don't meet your specific aggregation needs.
