Back
Close

Using C# LINQ - A Practical Overview

player_one
1386.4K views

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);
IEnumerable<string> strings = new List<string> { "a", "ab", "abc" };
var result = strings.Sum(str => str.Length);
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io