11.3. High-Level Goals
When the generics gurus sat down to spell out a strategy for their generics implementation, they established some very key high-level goals that, in a more general sense, shaped some of the resulting, fundamental structure of generics. The following is a brief breakdown of the guiding principles that appear to have played a key role in influencing the overall design of .NET generics.
11.3.1. Validate at Declaration
For any generics implementation, you must choose between validating generic types at their point of declaration or at their point of instantiation. So, if you define a List<T> generic type and you choose to validate it at the point of declaration, you essentially need to be able to verify that the List<T> generic type will be valid for all type arguments that it could ultimately be supplied as a type parameter.
In contrast, if you choose to validate at the point where each type is used, you are only required to verify that each individual specialization can be successfully compiled. Even if there are declarations that could be invalid, they are not factored into the validation of the class at compile-time. Only instances that are actually declared in code will be considered. Although this approach offers a somewhat broader palette of possibilities, it also requires the precompilation of all the existing instances in advance of their use.
For .NET generics, the decision was made to require validation at the point of declaration. The rationale ...
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.