Sunday, April 02, 2006

.NET: Introducing Generics in the CLR -- MSDN Magazine, Visual Studio 2005 Guided Tour

.NET: Introducing Generics in the CLR -- MSDN Magazine, Visual Studio 2005 Guided Tour: "Generics are a shipping feature of the Microsoft� .NET Framework 2.0, and managed code is faster, more maintainable, and more robust because of it.
Generics are an extension to the CLR's type system that allow developers to define types for which certain details are left unspecified. Instead, these details are specified when the code is referenced by consumer code. The code that references the generic type fills in the missing details, tailoring the type to its particular needs. The name generics reflects the goal of the feature: to enable the writing of code while not specifying details that might limit the scope of its usefulness. The code itself is generic. I'll get more specific in just a moment.

A First Look at Generics

As with any new technology, it is helpful to ask just why it's useful. Those of you who are familiar with templates in C++ will find that generics serve a similar purpose in managed code. However, I hesitate to draw too much of a comparison between CLR generics and C++ templates because generics have some additional benefits, as well as some limitations.
Some of the strengths of CLR generics are compile-time type safety, binary code reuse, performance, and clarity. I'll briefly describe these benefits, and as you read the rest of this article, you'll understand them in more detail. As an example let's take two hypothetical collection classes: SortedList, a collection of Object references, and GenericSortedList, a collection of any typeT."

No comments: