15,883,705 members
3.00/5 (1 vote)
See more:
Hi there,

I have an array in this format int[] i = {0,1,2,3,4,5,6,7,8,9};

Please help me with a procedure thatI can use to get the sum of the values in my array.

Eg.
int value=(0+1+2+3+4+5+6+7+8+9);
int sum= value;

Martin
Posted
Updated 17-Jul-11 3:05am
v2
Dalek Dave 17-Jul-11 9:05am

## Solution 1

there are many ways, you can do like this.

int sum = i.Sum();

v2
thatraja 17-Jul-11 8:38am
5!
Salmen Essridi 17-Jul-11 8:48am
Thank you.
Dalek Dave 17-Jul-11 9:04am
Simplest method!
Espen Harlinn 17-Jul-11 10:16am
Nice and simple, my 5
Sergey Alexandrovich Kryukov 18-Jul-11 3:52am
As easy as that, a 5.
--SA

## Solution 2

Try below code.

int[] i = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int sum = i.Sum();

thatraja 17-Jul-11 8:38am
5!
RaisKazi 17-Jul-11 9:05am
Thanks Man !!!

## Solution 3

The usual thing to do is to create an int to hold the total value and set it to 0.
The you do a for loop across each value in the array adding it to the total. Once the loop is done you have your total.

thatraja 17-Jul-11 8:37am
Classic way, 5!

## Solution 4

You can use LINQ to do this.
See an example here[^].

thatraja 17-Jul-11 8:37am
5!
Abhinav S 17-Jul-11 8:46am
Thank you.

## Solution 5

Another way,

C#
int[] intArray = { 1, 2, 3, 4, 5, 6 };
int result = 0;
Array.ForEach(intArray, item => result += item);

Hope it helps.

Abhinav S 18-Jul-11 4:10am
This is also another approach. Univote corrected.