C# VS2026
Lesson 08 of 30

Lists, Dictionaries and Sets

C# in Visual Studio 2026 — a hands-on guide for developers at every level.

List<T>

A List<T> is a resizable array. You can add and remove elements at any time.

var fruits = new List<string> { "apple", "banana", "cherry" };
fruits.Add("date");
fruits.Remove("banana");
fruits.Insert(1, "blueberry");

Console.WriteLine($"Count: {fruits.Count}");
Console.WriteLine(fruits.Contains("apple")); // True

foreach (var f in fruits)
    Console.WriteLine(f);

Dictionary<TKey, TValue>

A dictionary maps unique keys to values. Lookups are O(1) — extremely fast even with millions of entries.

var ages = new Dictionary<string, int>
{
    ["Alice"] = 30,
    ["Bob"]   = 25,
    ["Carol"] = 28
};

ages["Dave"] = 35;  // add new entry

if (ages.TryGetValue("Bob", out int bobAge))
    Console.WriteLine($"Bob is {bobAge}");

foreach (var kv in ages)
    Console.WriteLine($"{kv.Key}: {kv.Value}");

HashSet<T>

A HashSet<T> stores unique elements only and supports fast membership testing and set operations.

var setA = new HashSet<int> { 1, 2, 3, 4 };
var setB = new HashSet<int> { 3, 4, 5, 6 };

setA.IntersectWith(setB);  // setA = { 3, 4 }
setA.UnionWith(setB);      // setA = { 3, 4, 5, 6 }

Console.WriteLine(setA.Contains(5));  // True