Q:

C# program to find the sum of array elements using the predefine Sum() method of Queryable class

belongs to collection: C# Basic Programs | array programs

0

C# program to find the sum of array elements using the predefine Sum() method of Queryable class

All Answers

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

Program:

The source code to find the sum of array elements using the predefine method is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# program to find the sum of array element 
//using Sum() method of Queryable class.

using System;
using System.Linq;

class Sample
{
    static void Main()
    {
        int[] MyArray = { 40, 30, 20, 10, 50 };
        int sum = 0;
        
        sum = Queryable.Sum(MyArray.AsQueryable());

        Console.WriteLine("Sum of array elements: {0}", sum);
    }
}

Output:

Sum of array elements: 150
Press any key to continue . . .

Explanation:

In the above program, we created an array of integers. Then find the sum of all array elements using the Sum() method of Queryable class. The Sum() method returns an integer value. To use a Queryable class, we need to import the System.Linq method in our program.

 

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

total answers (1)

C# Basic Programs | array programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C# program to find the average of array elements u... >>
<< C# program to find the smallest and largest elemen...