5 min read
Collections in Depth
Prerequisites: Generics
var list = new List<string>(); // ordered, duplicates allowed
var set = new HashSet<string>(); // unique elements
var dict = new Dictionary<string, int>(); // key -> value pairs
dict["Ada"] = 36;
dict["Grace"] = 85;
Console.WriteLine(dict["Ada"]); // 36
Console.WriteLine(dict.ContainsKey("Bob")); // false
foreach (var pair in dict) {
Console.WriteLine($"{pair.Key} is {pair.Value}");
}
Directly parallel to Java's List/Set/Map: List<T> for
order and duplicates, HashSet<T> for uniqueness, Dictionary<K, V> for key-based lookup.