Managed C++ and .NET Development: Visual Studio .NET 2003 Edition

Overview

Anyone who has been around the coding world for any length of time has more than likely written her own collection routine—probably a simply linked list. Newer programmers may not have written one of their own, but instead, in the case of C++ programmers, used the Standard Template Library (STL) version of a link list. Either way, most programmers have found a need to work with collections. The .NET Framework uses collections as well. Because collections are so common, the .NET Framework class library provides a large number of different types.

The .NET Framework class library divides its generic collections into two different namespaces. The more common collections are in the System::Collections namespace. Those collections that are specialized or less common are in the System::Collections::Specialized namespace. Something to be aware of is that the two namespaces seem to imply that the specialized collections are inherited from the common, but in fact there is no such relationship. The namespaces are just groupings of different types of collections.

This chapter will focus on the generic collection set shown in Table 7-1. However, the .NET Framework class library has many other specific collections scattered throughout the many namespaces—for example, System::Text::RegularExpressions::Group, System::Web::UI::WebControls::DataKeyCollection, System::Security::PermissionSet, and even System::Array.

Table 7-1: .NET Collection Classes

COLLECTION

DESCRIPTION

ArrayList

An array that grows dynamically

BitArray

An array of bit values (either 1 or 0)

BitVector32

A small collection that will represent Boolean or small integers within 32 bits of memory

CollectionBase

An abstract base class for deriving strongly typed collections

DictionaryBase

An abstract base class for deriving strongly typed collections of key/value pairs

Hashtable

A collection of key/value pairs organized based on a hash code of the key

HybridDictionary

A collection that switches from a ListDictionary when small, to a Hashtable when large

ListDictionary

A singular link list recommended for lists of ten objects or less

NameValueCollection

A collection string of key/value pairs organized on the string key and accessible by either string key or index

Queue

A collection of first-in-first-out objects

SortedList

A collection of key/value pairs sorted by key and accessible by either key or index value

Stack

A collection of first-in-last-out objects

StringCollection

A collection of strings

StringDictionary

A Hashtable with the key strong typed to be a string

To make things easier for the developer, the .NET Framework class library provides a number of interfaces (see Table 7-2) that help provide some commonality between the collections. Learning collections is simplified because many of the collections share these interfaces, and once you learn an interface in one collection, it requires little effort to learn it in a second one.

Table 7-2: .NET Collection Interfaces

INTERFACE

DESCRIPTION

ICollection

Defines methods to determine the size of and enable thread safety for the collection

IComparer

Exposes a method to compare objects of the collection

IDictionary

Defines methods to allow access to key/value pairs within the collection

IDictionaryEnumerator

Exposes methods to access keys and values while enumerating a collection

IEnumerable

Exposes a method to retrieve an object that implements the IEnumerator interface

IEnumerator

Exposes a method to enumerate through a collection

IHashCodeProvider

Exposes a method to provide a custom hash algorithm

IList

Defines methods to add, insert, delete, and access objects using an index

Категории