C# Program to Calculate the Sum of Array Elements Using the LINQ Aggregate() Method

You are currently viewing C# Program to Calculate the Sum of Array Elements Using the LINQ Aggregate() Method

C# program to calculate the sum of array elements using the LINQ Aggregate() method

//C# program to calculate the sum of array elements 
//using the Linq Aggregate() method.

using System;
using System.Linq;

class LinqDemo
{
    static void Main(string[] args)
    {
        int[] values = { 10, 8, 2, 2, 7, 4};
        int sum = 0;
        
        sum = values.Aggregate((val1, val2) => val1 + val2);
        
        Console.WriteLine("Sum is : "+sum);
    }
}

Output

Sum is : 33
Press any key to continue . . .

Ranjith Kumar

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply