C# Foundations

Lesson 5 of 6

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:

LINQJavaScript 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.

📝 Collections & LINQ Quiz

Passing score: 70%
  1. 1.Which LINQ method keeps only the elements that match a condition?

  2. 2.A resizable, generic collection of integers is declared as List<____>.

  3. 3.LINQ methods like Where and Select modify the original collection in place.