11.4. IL Representation of Generic Types

To understand how generics are implemented within the .NET platform, you must first look at how they are represented in the IL that is managed by the CLR. The .NET approach to supporting generic types in IL plays a key role in determining how this IL will ultimately be transformed into executable code. The IL representation of generics also ends up heavily influencing many of the policies and optimizations that end up being associated with the overall implementation.

Because each generic type must be able to be fully validated at the point of its declaration, this also means that these generic types can be directly represented—in their generic form—in the CLR. A .NET generic class requires no advance awareness of instances that might be created with that class. This reality means that generic types can be treated more like traditional classes, where instances are simply created on an as-needed basis.

This idea of representing generic types at run-time is one of the key distinguishing characteristics of the .NET implementation. To get a better idea for what a generic type looks like, you need to look at the IL that gets generated for a generic type. To achieve that goal, though, you must first have a generic type to explore. For the purposes of this example, you'll start with the following simple generic class:

[VB code] Public Class TestClass(Of T) Private _data As T Public Sub New(ByVal data As T) Me._data = data End Sub Public Function ...

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.