Q:

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

belongs to collection: C# LINQ Programs

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program:

The source code to calculate the sum of array elements using the Aggregate() method, is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//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 . . .

Explanation:

In the above program, we created LinqDemo class that contains the Main() method. In the Main() method we created an array of integers and then calculate the sum of all array elements using the Aggregate() method and then print the sum on the console screen. 

To use the Aggregate() method, it is mandatory to import "System.Linq" namespace.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C# LINQ Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C# program to calculate the total of employee\... >>
<< C# program to demonstrate the example of Linq Aggr...