11.5. Specialization and Sharing

Okay, so now you have a better idea of what a generic type looks like in IL. The next concept to consider here is how instances of these generic types are managed by the CLR. The process of translating generic types into specific instances is at the epicenter of every generics implementation. Naturally, it's safe to assume that the strategies and policies that are adopted for this translation heavily influence the overall footprint and performance characteristics of any generics implementation. To understand what's at stake here, let's start by considering the two models that have been traditionally employed by prior generics-based solutions.

11.5.1. Code Specialization

Code specialization is the term used to describe the creation of specialized types for each instance of a generic type. If you have declared a generic class, say List<T>, then for each instance of this class that appears in your code (List<int>, List<string>, and so on) the complier will actually generate a unique, standalone specialization for each of these types. This is the strategy that is employed, for example, by C++ templates and is typically criticized for bloating the run-time representation of generic types. It is also lauded for its support for meta-programming models, its handling of value types, and some of the general performance gains it achieves.

11.5.2. Code Sharing

Code sharing is the term used to describe the model where generic instances share their underlying ...

Get Professional .NET 2.0 Generics now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.