Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Sir,

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

Please help me with a method that I can use to get the sum of the values in my array.


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

Thank you.
Posted

A little bit of LINQ
C#
int[] i = { 0, 1, 2, 3, 4, 5, 6 };
var sum = i.Sum();


or "classic"

C#
int s = 0;

foreach (var item in i)
{
    s += item;
}


Is this so difficult?
 
Share this answer
 
v3
Comments
Sandeep Mewara 14-May-12 10:04am    
5!
Mohammad A Rahman 14-May-12 10:06am    
Exactly :)
Andy411 14-May-12 15:56pm    
Thx to everybody voting it high :-)
In addition to the Solution 1, it might help you to understand more about Sum from How does it work in C#? - Part 3 (C# Linq in detail)[^]

Thanks :)
 
Share this answer
 
Comments
Andy411 14-May-12 16:01pm    
Thx for the link to your article. That's my 5 right here. I need to take me a bit time to read it in the next days.
Mohammad A Rahman 15-May-12 6:54am    
Thank you Andy :) You are most welcome to read my article :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900