Collections and LINQ
using System.Linq;
List<int> scores = new List<int> { 80, 92, 67, 100 };
var passed = scores.Where(s => s >= 70).ToList();
var doubled = scores.Select(s => s * 2).ToList();
int total = scores.Sum();
Console.WriteLine($"passed: {string.Join(", ", passed)}");
Console.WriteLine($"total: {total}");
Collections
List<T> is a resizable, generic collection, the <int> says it only holds ints.
LINQ
LINQ (Language Integrated Query) adds query-style methods to collections, similar to JavaScript's map/filter/reduce:
| LINQ | JavaScript equivalent |
|---|---|
Where(predicate) | filter |
Select(transform) | map |
Sum() | reduce (adding) |
LINQ methods live in the System.Linq namespace, and (like the JavaScript array methods they resemble) return a new sequence rather than modifying the original.