I have an int array like this
int[] arr = {256741038,623958417,467905213,714532089,938071625};
and then I created an int64 var
Int64 sum = arr.Sum();
But this reslted in an overflow
Run-time exception (line 19): Arithmetic operation resulted in an overflow.
How can I solve this problem without using loop to sum it up ? (array type must be int)
arr.Select(z => (long)z).Sum()Sumdoesn't choose the return type based on what is needed to store theSum(which, in this case would be along). Instead, it chooses it based on the type of the enumerable being summed. You are summingintso, alas, it tries (unsuccessfully) to fit the sum into anint.