Aggregate with seed
In this chapter you will learn:
Seeded aggregations
You can omit the seed value when calling Aggregate. The first element becomes the implicit seed, and aggregation proceeds from the second element.
using System;//from jav a2 s .c om
using System.Collections;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
int[] numbers = { 1, 2, 3 };
int x = numbers.Aggregate(0, (prod, n) => prod * n); // 0*1*2*3 = 0
int y = numbers.Aggregate((prod, n) => prod * n); // 1*2*3 = 6
Console.WriteLine(x);
Console.WriteLine(y);
}
}
The output:
sum with seed
using System;//j av a2 s .com
using System.Collections;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
int[] numbers = { 2, 3, 4 };
int sum = numbers.Aggregate(0, (total, n) => total + n); // 9
Console.WriteLine(sum);
}
}
The output:
Next chapter...
What you will learn in the next chapter:
Home » C# Tutorial » Linq Operators