SUMMARY

This chapter explained several types of collection classes.

Variable arrays store objects sequentially. They allow fast access at any point in the array. The Array class lets you make arrays indexed with nonzero lower bounds, although they provide slower performance than arrays of variables, which require lower bounds of zero. The Array class provides several useful methods for working with Array objects and normal variable arrays, including Sort, Reverse, IndexOf, LastIndexOf, and BinarySearch.

Collections store data in ways that are different from those used by arrays. An ArrayList stores items in a linked list. That works well for short lists, but slows down when the list grows large. A StringCollection holds a collection of strings. StringCollection is an example of a strongly typed collection (it holds only strings). The NameValueCollection class is a specialized collection that can hold more than one string value for a given key value.

Dictionaries associate key values with corresponding data values. You look up the key to find the data much as you might look up a word in the dictionary to find its definition. The ListDictionary class stores its data in a linked list. It is fast for small data sets but slow when it contains too much data. In contrast a Hashtable has substantial overhead but is extremely fast for large dictionaries. A HybridDictionary acts as a ListDictionary if it doesn’t contain too much data and switches to a Hashtable when it gets too big. The StringDictionary class is basically a Hashtable that is strongly typed to work with strings. The SortedList class is a Hashtable/Array hybrid that lets you access values by key or in sorted order.

Stack classes provide access to items in last-in-first-out (LIFO) order. Queue classes give access to their items in first-in-first-out (FIFO) order.

The generic Dictionary, LinkedList, List, Queue, SortedDictionary, SortedList, and Stack classes enable you to use strongly typed data structures.

Although these classes have very different features for adding, removing, finding, and ordering objects, they share some common traits. For example, those that provide an Add method support collection initialization. They also all support enumeration by For Each statements.

These classes provide many useful features so you can pick the class that best satisfies your needs. Deciding which class is best can be tricky, but making the right choice can mean the difference between programs that process a large data set in seconds, hours, or not at all. Spend some time reviewing the different characteristics of the classes so that you can make the best choice possible.

This chapter explained how you can use the generic collection classes provided by the System.Collections.Generic namespace. Chapter 26, “Generics,” explains how you can build generic classes of your own. Using generics, you can build strongly typed classes that manipulate objects of any data type.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.225.95.107