Library reference

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
Program.cs
using System;
using System.Linq;

var values = new int[] { 1, 2, 3 };
Console.WriteLine(values.Sum());
Output
6
Advertisements

Everyday examples

Add the three amounts on a receipt.

Start5 · 5 · 8
Result18
Program.cs
using System;
using System.Linq;

var values = new int[] { 5, 5, 8 };
Console.WriteLine(values.Sum());
Output
18

How many passengers boarded at the three stops?

Start2 · 7 · 1
Result10
Program.cs
using System;
using System.Linq;

var values = new int[] { 2, 7, 1 };
Console.WriteLine(values.Sum());
Output
10

How many exercise books arrived in three boxes?

Start10 · 8 · 9
Result27
Program.cs
using System;
using System.Linq;

var values = new int[] { 10, 8, 9 };
Console.WriteLine(values.Sum());
Output
27
Run the example
Terminal
dotnet new console --output Lesson
cp Program.cs Lesson/Program.cs
dotnet run --project Lesson
Official documentation