CHAPTER 9 COLLECTIONS AND GENERICS
Note There are two additional collection-centric namespaces (System.Collections.ObjectModel and
System.Collections.Concurrent) in the .NET base class libraries. You will examine the former namespace later
in this chapter, after you are comfortable with the topic of generics. System.Collections.Concurrent provides
thread-safe collection classes (see Chapter 19 for information on multithreading).
The Problems of Nongeneric Collections
While it is true that many successful .NET applications have been built over the years using these
nongeneric collection classes (and interfaces), history has shown that use of these types can result in a
number of issues.
The first issue is that using the System.Collections and System.Collections.Specialized classes
can result in some poorly performing code, especially when you are manipulating numerical data (e.g.,
value types). As you’ll see momentarily, the CLR must perform a number of memory transfer operations
when you store structures in any nongeneric collection class prototyped to operate on System.Objects,
which can hurt runtime execution speed.
The second issue is that most of the nongeneric collection classes are not type safe because (again)
they were developed to operate on System.Objects, and they could therefore contain anything at all. If a
.NET developer needed to create a highly type-safe collection (e.g., a container that can hold objects
implementing only a certain interface), the only real choice was to create a brand new collection class by
hand. Doing so was not too labor intensive, but it was a tad bit on the tedious side.
Before you look at how to use generics in your programs, you’ll find it helpful to W