Advertisement
Beginner C#, Generics

What are generics and why use them?

Short answer

Generics let you write type-parameterised classes/methods that work with any type while keeping compile-time type safety and avoiding boxing.

Save C# mock interview
Advertisement
AModel 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.