
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
217
Chapter 4
CHAPTER 4
Generics
4.0 Introduction
A long-awaited feature, generics, is finally here with the advent of Version 2.0 of the
C# compiler. Generics is an extremely useful feature that allows you to write less,
but more efficient, code. This aspect of generics is detailed more in Recipe 4.1. With
generics comes quite a bit of programming power, but with that power comes the
responsibility to use it correctly. If you are considering converting your
ArrayList,
Queue, Stack, and Hashtable objects to use their generic counterparts, consider read-
ing Recipes 4.4, 4.5, and 4.10. As you will read, the conversion is not always simple
and easy, and there are reasons why you might not want to do this conversion at all.
Other recipes in this chapter deal with other generic classes contained in the .NET
Version 2.0 Framework, such as Recipe 4.6. Still others deal with the operation of
any generic type, such as Recipes 4.2, 4.8, and 4.13.
4.1 Deciding When and Where to Use Generics
Problem
You want to use generic types in a new project or convert nongeneric types in an
existing project to their generic equivalent. However, you do not really know why
you would want to do this, and you do not know which nongeneric types should be
converted to be generic.
Solution
In deciding when and where to