Caffeinated Coder
Book an appointment
Lesson 13 · Working with data

Generics

Generics let you write a class or method once and use it with any type, without losing type checks. Follow one generic class, Box<T>, as Program.cs uses it twice: the type arguments int and string replace the type parameter T, the compiler makes the constructed types Box<int> and Box<string>, catches wrong types before anything runs, and at run time the program prints 42 hi.

12 min11 steps5 questionsIntermediate
Fig 13 · Interactive
Loading figure…
or ← →

Quiz Unlocks after the last step.