๐ WAYPOINT LESSON
Generic collections are the payoff
โญ beginnerโณ 11 min read๐ Lesson 48 of 85
`List<T>`, `Dictionary<TKey, TValue>`, `Queue<T>`, `Stack<T>` โ the BCL is built from the same tool you just learned.
Reading the signatures
public class List<T> { ... }
public class Dictionary<TKey, TValue> { ... }
The collections you've used since module 8 are generic classes. Their type parameters are why list[0] returns T โ not object you must cast โ and why putting a string into a List<int> is a compile error, not a runtime surprise.
Why not object?
Pre-generics collections stored object (the base of everything):
// the old disease, still legal:
var list = new ArrayList();
list.Add(42);
int x = (int)list[0]; // cast needed, checked at RUNTIME
list.Add("oops"); // compiles fine โ boom later
Value types stored in object also box (heap-allocate) on every add. Generics eliminate the casts, move errors to compile time, and skip the boxing. That trio โ no casts, compile-time safety, no boxing โ is the entire point.
โก Now practice
Ready to CodeGenerics workoutSwaps, stacks, pair stores, and a count-if โ type parameters under discriminating tests.
4 challenges ยท ยท ~40 min