This post will discuss how to calculate the sum of all elements in an integer array in C#.

1. Using Enumerable.Sum() method

We can make use of the built-in numeric aggregation method Sum() from the System.Linq namespace to compute the sum of numeric values in a sequence. This is demonstrated below:

Download  Run Code

2. Using Array.ForEach() method

Using the Array.ForEach() method, we can perform the addition operation on each element of the specified array. The following example demonstrates this by finding the total sum of all the array elements using Array.ForEach():

Download  Run Code

3. Using foreach loop

We can also loop through the array elements using the foreach statement and compute the sum on the fly. This is demonstrated below:

Download  Run Code

4. Using Enumerable.Aggregate() method

Finally, one can use the Enumerable.Aggregate() method in System.Linq Namespace, which applies an accumulator function on each element of a sequence.

The following code example demonstrates how to use Aggregate to perform addition.

Download  Run Code

That’s all about calculating the sum of all elements of an array in C#.