7.7. Are Generics Generic?

Some would say that the introduction of constraints makes the .NET generics implementation anything but generic. As best as I can tell, this argument seems to emanate primarily from those who have strong ties to templates. And, from that perspective, where a type parameter is truly a placeholder for any type, I can see how that would lead one to conclude that constraints somehow detract from the broader definition of a generic type. In that context, being a generic type means having no constraints on your type parameters and the freedom to view those types as being completely devoid of any rules that would impose requirements on their shape or structure. If you share this view, you are likely to believe that generics aren't generic.

The .NET implementation of generics introduced constraints as a tradeoff. Supporting the existing C++ templates model for generics would have required generics to support a compile-time model, where each constructed type would be created as part of compilation. This would have been the only way to retain these fully generic types and still verify that each instance of parameter substitution was fully resolvable. This was certainly a valid option. However, it also brought with it the baggage that is associated with the templates model, including code bloat (see Chapter 3 "Generics ≠ Templates," for a complete comparison of generics and templates). The generics designers wanted to overcome some of the traditional templates-related ...

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.