2.1. Motivation

Types have to matter. With every class you write, you need to be focused on how that class represents itself to clients. Each time clients touch the interfaces of your class, they are binding to the specific types exposed in the signature of that interface. As such, you need to be concerned about the type-safety implications that accompany each of these interactions. Does your interface provide a clear set of types that make every attempt to eliminate ambiguity, or does your interface favor generality at the expense of type safety?

From my perspective, a great deal of what generics has to offer is focused squarely on allowing you to achieve a much greater level of type safety without having to compromise on generality (or bloat your code with more specialized classes). Generics should, in some respects, force you to apply a higher standard to the classes you write and consume. They should put you in a position where you look at the type safety of each interface with a significantly higher level of scrutiny than you might have in the pre-generics era.

As best I can tell, this fundamental mindset is sometimes lost in the discussions surrounding generics. Whenever developers look at a new language feature, they often ask, "What new functionality can I build with this feature that I couldn't build before?" Though generics do enable new capabilities, that's not the point. Generics aren't just about doing something new—they're about doing something better. Through generics, ...

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.