The principal benefit of collections is that they standardize the way groups of objects are handled by your programs. All collections are designed around a set of clearly defined interfaces. Several built-in implementations of these interfaces, such as ArrayList, Hashtable, Stack, and Queue, are provided, which you can use as-is. You can also implement your own collection, but you will seldom need to.
The .NET Framework supports four general types of collections: non-generic, specialized, bit based, and generic. The first three have been part of C# since version 1.0. The generic collections were added by C# 2.0.
The non-generic collections implement several fundamental data structures, including a dynamic array, stack, and queue. They also include dictionaries, in which you can store key/ value pairs. An essential point to understand about the non-generic collections is that they operate on data of type object. Thus, they can be used to store any type of data, and different types of data can be mixed within the same collection. Of course, because they store object references, they are not type-safe. The non-generic collection classes and interfaces are in System.Collections.
The specialized collections operate on a specific type of data or operate in a unique way. For example, there are specialized collections for strings. There are also specialized collections that use a singly linked list. The specialized collections are declared in System.Collections.Specialized.
C# defines one bit-based collection called BitArray. BitArray supports bitwise operations on bits, such as AND and XOR. As such, it differs significantly in its capabilities from the other collections. BitArray is declared in System.Collections.
The generic collections provide generic implementations of several standard data structures, such as linked lists, stacks, queues, and dictionaries. Because these collections are generic, they are type-safe. This means that only items that are type-compatible with the type of the collection can be stored in a generic collection, thus eliminating accidental type mismatches. Generic collections were added by C# 2.0 and are declared in System.Collections.Generic.
There are also three classes in the System.Collections.ObjectModel namespace that support programmers who want to create their own generic collections.
Fundamental to all collections is the concept of an enumerator, which is supported by the IEnumerator and IEnumerable interfaces. An enumerator provides a standardized way of accessing the elements within a collection, one at a time. Thus, it enumerates the contents of a collection. Because each collection must implement IEnumerable, the elements of any collection class can be accessed through the methods defined by IEnumerator. Therefore, with only small changes, the code that cycles through one type of collection can be used to cycle through another. As a point of interest, the foreach loop uses the enumerator to cycle through the contents of a collection.
A feature related to IEnumerator and IEnumerable is the iterator. Iterators were added by C# 2.0. They simplify the process of creating classes, such as custom collections, that can be cycled through by a foreach loop. Iterators are also described in this chapter.
One last thing: If you are familiar with C++, then you will find it helpful to know that C# collection classes are similar in spirit to the Standard Template Library (STL) classes defined by C++. What C++ calls a container, C# calls a collection. The same is true of Java. If you are familiar with Java's Collections Framework, then you will have no trouble learning to use C# collections.
Because of the differences between the four types of collections-non-generic, bit-based, specialized, and generic-this chapter discusses each separately.
No comments:
Post a Comment