[Solved] Is it possible to sum all integers in a list? (C# console) [duplicate]


The other answer recommends LINQ, and that is definitely the less verbose way to do it. But I have a feeling that you’re just learning, so may want to have an idea of how this actually works. Below is the an abridged version of the Linq.Sum source code so you can see what is happening.

public static int Sum(this IEnumerable<int> source)
{
    int sum = 0;
    foreach (int v in source)
    {
        sum += v;
    }
    return sum;
}

It loops through each item in the IEnumerable, adds them to another variable, and then returns that result.

solved Is it possible to sum all integers in a list? (C# console) [duplicate]