C# › Generics

What are generics and why use them?

Beginner

Answer

Generics let you write type-parameterised classes/methods that work with any type while keeping compile-time type safety and avoiding boxing.
public class Box<T> { public T Value { get; set; } }
var b = new Box<int> { Value = 5 };
Real-world example List<T> and Dictionary<K,V> give type-safe collections without casts.