Three ready-made jobs
Finding the largest value and adding numbers up are jobs a loop can do, and they come up so often that LINQ has them built in. For a List<int> named nums:
nums.Min()returns the smallest value.nums.Max()returns the largest value.nums.Sum()returns the total of all the values.
List<int> prices = new List<int>(new[] {4, 9, 2, 7});
Console.WriteLine(prices.Min()); // 2
Console.WriteLine(prices.Max()); // 9
Console.WriteLine(prices.Sum()); // 22
These are LINQ methods, like Where and Select, so a file of your own needs using System.Linq; at the top. The lesson's editor already has it. They work on anything you can loop over, so an array behaves the same way: for int[] arr = {4, 9, 2, 7}, the calls arr.Min(), arr.Max() and arr.Sum() give 2, 9 and 22.
Min() versus Math.Min
Math.Max(a, b) compares exactly two values. Max() on a list looks at all of them. Both exist, and they do different jobs.
What they replace
Each call stands in for a loop you have already written: a variable that starts at the first value and is updated as you go for Min() and Max(), and a total that starts at 0 and grows by each value for Sum(). The built-in version is shorter and has no starting value to get wrong, such as starting a maximum at 0 when every number is negative. A loop is still the right tool when you need more than one of these in a single pass, or when you must also know where the smallest value is.
Gotchas
- An empty list. It has no smallest and no largest value, so
Min()andMax()throw anInvalidOperationExceptionwith the message "Sequence contains no elements".Sum()of an empty list is 0. If a list might be empty, checknums.Count > 0first. - Overflow in
Sum(). Plainintarithmetic wraps around silently, butSum()onints checks for overflow.new List<int>{2000000000, 2000000000}.Sum()throws anOverflowException. Uselongvalues, orSelect(n => (long)n).Sum(), if totals can be that large. - Forgetting the parentheses.
prices.Min()is a method call.prices.Minwithout them is a different thing and does not compile as a number.
The lesson's task
minutes holds how many minutes someone practiced coding on each day of a week. The task is to store the shortest day, the longest day and the week's total in three int variables, and the code at the bottom prints them. Each one is one call, with no loop:
List<int> minutes = new List<int>(new[] {25, 40, 15, 60, 30, 45, 20});
int shortest = minutes.Min();
int longest = minutes.Max();
int total = minutes.Sum();
Console.WriteLine("shortest = " + shortest);
Console.WriteLine("longest = " + longest);
Console.WriteLine("total = " + total);
It prints:
shortest = 15
longest = 60
total = 235
Step by step
- Below the list, declare
int shortest = minutes.Min();. One call looks at every value. - Declare
int longest = minutes.Max();the same way. - Declare
int total = minutes.Sum();. The three lines that print the results are already in the starter code and use these three names exactly.
The shortest day is 15 minutes, the longest is 60, and the seven days add up to 235.
Key takeaways
Min(),Max()andSum()replace a loop with one call.- They need
using System.Linq;and work on lists and arrays. Min()andMax()throw on an empty list, whileSum()returns 0.
