Yes, you can use Sumlike this. Just assign the result to something:
int s = intList.Sum();
Now you have the sum of the numbers in the list stored in a variable called s.
This is actually an extension method added in .NET 3.5, and it works not only with lists. Enumerable.Sum
Of course, internally this still goes through the numbers and adds them, but it is much easier to name this method than to write it yourself. Using the .NET Reflector you can see that the implementation is quite simple:
public static int Sum(this IEnumerable<int> source)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
int num = 0;
foreach (int num2 in source)
{
num += num2;
}
return num;
}
, Queryable.Sum. , . , .