CSharp - Use Aggregate selector to do the sum

Description

Use Aggregate selector to do the sum

Demo

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
class Program//from  ww  w  . ja va2 s .c  om
{
    static void Main(string[] args)
    {
        IEnumerable<int> intSequence = Enumerable.Range(1, 10);
        
        //  I'll just output the sequence so all can see it.
        foreach (int item in intSequence)
          Console.WriteLine(item);
        Console.WriteLine("--");
        
        //  Now calculate the sum and display it.
        int sum = intSequence.Aggregate(0, (s, i) => s + i);
        Console.WriteLine(sum);
    }
}

Result

Related Topic