Advertisement
Advanced C#, Generics

How are generics implemented for value types vs reference types in the CLR?

Short answer

The JIT creates a specialised instantiation per value type (no boxing, better perf) but shares a single instantiation for all reference types, since they are the same size (a pointer).

Save C# mock interview
Advertisement
AModel answer
The JIT creates a specialised instantiation per value type (no boxing, better perf) but shares a single instantiation for all reference types, since they are the same size (a pointer).
Example

List<int> // dedicated code, no boxing List<string>, List<object> // share one instantiation

Real-world example
This is why List<int> outperforms ArrayList — no boxing per element.