Collection Interfaces and Types
Collection classes can be grouped into
collections that store elements of type Object and generic collection classes. Previous to
.NET 2.0, generics didn’t exist. Now the generic collection classes
usually are the preferred type of collection. Generic collection
classes are type-safe, and there is no boxing if value types are
used. You only need object-based collection classes if you want to
add objects of different
types where the types are not based on each other, for example,
adding int and string objects to a collection. Another group of
collection classes is collections specialized for a specific type;
for example, the StringCollection class
is specialized for the string type.
Object-type collections are located in the
namespace System.Collections; generic
collection classes can be found in the namespace System.Collections.Generic. Collection classes that
are specialized for a specific type are located in the namespace
System.Collections.Specialized.
Of course, there are also other ways to group
collection classes. Collections can be grouped into lists,
collections, and dictionaries based on the interfaces that are
implemented by the collection class. Interfaces and their
functionality are described in the following table. .NET 2.0 adds
new generic interfaces for collection classes, for example,
IEnumerable<T> and IList<T>. While the nongeneric versions of
these interfaces define an Object as
parameter of the methods, the generic version of these interfaces
use the generic type T.
|
|
Tip |
You can read detailed information about the
interfaces IEnumerable, ICollection, and IList in
Chapter 5, “Arrays.”
|
Interfaces and their methods and properties that
are important for collections are described in the following
table.
|