Add the values in a list
Add every item once. Repeated values still count each time they occur.
Enumerable.Sum
Simple example
Add one, two and three.
Start1 · 2 · 3
Result6
using System;
using System.Linq;
var values = new int[] { 1, 2, 3 };
Console.WriteLine(values.Sum());
6
Advertisements
Everyday examples
Add the three amounts on a receipt.
Start5 · 5 · 8
Result18
using System;
using System.Linq;
var values = new int[] { 5, 5, 8 };
Console.WriteLine(values.Sum());
18
How many passengers boarded at the three stops?
Start2 · 7 · 1
Result10
using System;
using System.Linq;
var values = new int[] { 2, 7, 1 };
Console.WriteLine(values.Sum());
10
How many exercise books arrived in three boxes?
Start10 · 8 · 9
Result27
using System;
using System.Linq;
var values = new int[] { 10, 8, 9 };
Console.WriteLine(values.Sum());
27
Run the example
dotnet new console --output Lesson
cp Program.cs Lesson/Program.cs
dotnet run --project Lesson