Section 26.2. Collections Overview


26.2. Collections Overview

All the collection classes in the .NET Framework implement some combination of the collection interfaces. These interfaces declare the operations to be performed on various types of collections. Figure 26.1 lists some of the interfaces of the .NET Framework collections. All the interfaces in Fig. 26.1 are declared in namespace System. Collections and have generic analogues in namespace System. Collections.Generic. Implementations of these interfaces are provided within the framework. You may also create your own custom implementations.

Figure 26.1. Some common collection interfaces.

Interface

Description

ICollection

The root interface in the collections hierarchy from which interfaces IList and IDictionary inherit. Contains a Count property to determine the size of a collection and a CopyTo method for copying a collection's contents into a traditional array.

IList

An ordered collection that can be manipulated like an array. Provides an indexer for accessing elements with an Integer index. Also has methods for modifying and searching a collection, including Add, Remove, Contains and IndexOf.

IDictionary

A collection of values, indexed by an arbitrary "key" object. Provides an indexer for accessing elements with an Object index and methods for modifying the collection (e.g., Add, Remove). IDictionary property Keys contains the Objects used as indices, and property Values contains all the stored Objects.

IEnumerable

An object that can be enumerated. This interface contains exactly one method, GetEnumerator, which returns an IEnumerator object (discussed in Section 26.3). ICollection implements IEnumerable, so all collection classes implement IEnumerable directly or indirectly.


Earlier versions of the .NET Framework provided the collection classes in the System.Collections and System.Collections.Specialized namespaces. These classes stored and manipulated Object references. You could store any Object in a collection. One inconvenient aspect of storing Object references occurs when retrieving them from a collection. An application normally needs to process specific types of objects, so the Object references obtained from a collection typically need to be downcast to an appropriate type.

The .NET Framework 2.0 now also includes the System.Collections.Generic namespace, which uses the generics capabilities we introduced in Chapter 25. Many of these new classes are simply generic counterparts of the classes in the System.Collections namespace. This means that you can specify the exact type that will be stored in a collection. You also receive the benefits of compile-time type checkingthe compiler ensures that you are using appropriate types with your collection and, if not, issues compile-time error messages. Also, once you specify the type stored in a collection, any item you retrieve from the collection will have the correct type. This eliminates the need for explicit type casts that can throw InvalidCastExceptions at execution time if the referenced object is not of the appropriate type. This also eliminates the overhead of explicit casting, improving efficiency.

In this chapter, we demonstrate six collection classesArray, ArrayList, Stac, Hashtable, generic SortedDictionary and generic LinkedListplus built-in array capabilities. Namespace System.Collections provides several other data structures, including BitArray (a collection of true/false values), Queue and SortedList (a collection of keyvalue pairs that are sorted by key and can be accessed either by key or by index). Figure 26.2 summarizes many of the collection classes. We also discuss the IEnumerator interface. Collection classes can create enumerators that allow you to walk through the collections. Although these enumerators have different implementations, they all implement the IEnumerator interface so thatthey can be processed polymorphically. As we will soon see, the For Each statement is simply a convenient notation for using an enumerator. In the next section, we begin our discussion by examining enumerators and the collections capabilities for array manipulation.

Figure 26.2. Some collection classes of the .NET Framework.

Class

Implements

Description

System namespace:

Array

IList

The base class of all conventional arrays. See Section 26.3.

System.Collections namespace:

ArrayList

IList

Mimics a conventional array, but will grow or shrink as needed to accommodate the number of elements. See Section 26.4.1.

BitArray

ICollection

A memory-efficient array of Booleans.

Hashtable

IDictionary

An unordered collection of keyvalue pairs that can be accessed by key. See Section 26.4.3.

Queue

ICollection

A first-in, first-out (FIFO) collection. See Section 24.6.

SortedList

IDictionary

An ordered collection of keyvalue pairs that sorts data by keys and can be accessed either by key or by index.

Stack

ICollection

A last-in, first-out (LIFO) collection. See Section 26.4.2.

System.Collections.Generic namespace:

Dictionary(Of K, E)

IDictionary(Of K, E)

A generic, unordered collection of keyvalue pairs that can be accessed by key.

LinkedList(Of E)

ICollection(Of E)

A generic doubly linked list. See Section 26.5.2.

List(Of E)

IList(Of E)

A generic ArrayList.

Queue(Of E)

ICollection(Of E)

A generic Queue.

SortedDictionary _ (Of K, E)

IDictionary(Of K, E)

A generic Dictionary that sorts data by the keys in a binary tree. See Section 26.5.1

SortedList(Of K, E)

IDictionary(Of K, E)

A generic SortedList.

Stack(Of E)

ICollection(Of E)

A generic Stack.

[Note: All collection classes directly or indirectly implement ICollection and IEnumerable (or the equivalent generic interfaces ICollection(Of E) and IEnumerable(Of E)).]




Visual BasicR 2005 for Programmers. DeitelR Developer Series
Visual Basic 2005 for Programmers (2nd Edition)
ISBN: 013225140X
EAN: 2147483647
Year: 2004
Pages: 435

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net