How to sum up an array of integers in C# How to sum up an array of integers in C# arrays arrays

How to sum up an array of integers in C#


Provided that you can use .NET 3.5 (or newer) and LINQ, try

int sum = arr.Sum();


Yes there is. With .NET 3.5:

int sum = arr.Sum();Console.WriteLine(sum);

If you're not using .NET 3.5 you could do this:

int sum = 0;Array.ForEach(arr, delegate(int i) { sum += i; });Console.WriteLine(sum);