Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

Sum() method

Without a parameter, the Sum() method can only be called on a sequence containing numerical elements. It returns a single value indicating the sum of all elements in the sequence. The type of the value returned matches the data type of the elements in the sequence.

IEnumerable<int> ints = new List<int> { 2, 2, 4, 6 };
// Will return 14
int result = ints.Sum();

Sum(<selector>) method

The Sum() method can also be called with a selector delegate parameter. The provided delegate should take a parameter of type T and return a numerical value. The result of the Sum() call will be the summation of all of these numerical values, as calculated for each element in the sequence. The type of the value returned from Sum() matches the data type of the of the value returned from the selector.

IEnumerable<int> ints = new List<int> { 2, 2, 4, 6 };
// Will return 60
int result = ints.Sum(val => val * val);

Sum() quiz

IEnumerable<string> strings = new List<string> { "a", "ab", "abc" };
var result = strings.Sum(str => 2.3F);
What value and data type would the Sum() method return in the code snippet above?
IEnumerable<string> strings = new List<string> { "a", "ab", "abc" };
var result = strings.Sum(str => str.Length);
What value and data type would the Sum() method return in the code snippet above?
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content