Generics in .NET 2.0.
Generics is a mechanism to support multiple ("generic") types. For example, you are creating a Stack data structure. What
kinds of objects would you want to store on the stack: Integers, strings, Cars? In C# 1.x you have to use an untyped container that stores object items and requires conversions between the type you store and object every time an item is inserted or removed from the Stack. There are two disadvantages to storing items as objects (VARIANTS in Visual Basic). Firstly, casting between object and and the type is computationally expensive. (See http://CshaprComputing.com/Tutorials/Lesson23.htm for benchmarks). Secondly, the resulting container is untyped which results in even slower code due to run time exceptions C# 2.0. (Whidbey) introduces generics that support creation of typed data structures. Example below (illustrates how to build a linked list with Generics. MyList object contains a list of elements of type T and supports inserting, extracting and removing elements from the list, as well as, enumeration of list elements. If you have experience with C++ templates abhor the difficulties of template debugging C++ template code difficult, do not fret C# generics. Type resolutions in C# generics is at compile time, so when you run this program through the debugger, MyList will appear as an List that accepts only integers.
Generics in .NET 2.0
aleksey on 12.16.04 @ 10:48 PM PST [link]