How to sum up an array of integers in C#
Posted
by Filburt
on Stack Overflow
See other posts from Stack Overflow
or by Filburt
Published on 2010-03-10T18:06:45Z
Indexed on
2010/03/14
21:25 UTC
Read the original article
Hit count: 264
Is there a better shorter way than iterating over the array?
int[] arr = new int[] { 1, 2, 3 };
int sum = 0;
for (int i = 0; i < arr.Length; i++)
{
sum += arr[i];
}
clarification:
Better primary means cleaner code but hints on performance improvement are also welcome. (Like already mentioned: splitting large arrays).
It's not like I was looking for killer performance improvement - I just wondered if this very kind of syntactic sugar wasn't already available: "There's String.Join - what the heck about int[]?".
© Stack Overflow or respective owner