AnyCompiler

Collections and LINQ

LINQ (Language Integrated Query) lets you chain operations like Where and Select directly over any collection, reading almost like a description of the transformation itself — keep the even numbers, then square them.

using System;
using System.Collections.Generic;
using System.Linq;

class Program {
    static void Main() {
        var numbers = new List<int> { 1, 2, 3, 4, 5, 6 };
        var evenSquares = numbers.Where(n => n % 2 == 0).Select(n => n * n);
        Console.WriteLine(string.Join(",", evenSquares));
    }
}
Output
4,16,36

Dictionary<K, V>

Dictionary<K, V> stores key-value pairs — indexing with [key] both inserts and retrieves, with the compiler enforcing that every key and value matches the declared types.

using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        var ages = new Dictionary<string, int>();
        ages["Alice"] = 30;
        ages["Bob"] = 25;
        Console.WriteLine(ages["Alice"]);
    }
}
Output
30

Next: delegates and events — storing a method itself in a variable, and C#'s dedicated publish/subscribe mechanism.

Practice exercise

Given a List<int> numbers containing 1, 2, 3, 4, use LINQ's Where and Select to build a list of the squares of the even numbers, then print it joined by commas.