3 ms·
> This is an oversimplification to say this but at some level generics are implemented as syntatic sugar for typecasts between Object and the type parameter ty
by saucetenuto 16y ago
> This is an oversimplification to say this but at some level generics are implemented as syntatic sugar for typecasts between Object and the type parameter type.
This is not true. Java generics work this way (google "type erasure"), but C# generics are implemented with specialized code, so that e.g. List<int>.Add has its own IL implementation distinct from the one for List<Bitmap>.Add. This lets the JIT do type-specific optimizations, like not having to box the ints in a List<int>.
The specialization code also tries to share code in cases where the type parameters don't matter (e.g. so that you don't have a million identical implementations of List.Count hanging around).