Summary


This chapter describes five types of objects: arrays, collections, dictionaries, stacks, and queues. It also explains how to convert any of these into strongly typed classes and how to use generic collections.

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. A Hashtable, on the other hand, 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, while 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 without going to the trouble of building your own strongly typed classes.

All of these classes, plus other strongly typed collections that you can derive from the CollectionBase, DictionaryBase, and other classes, provide significant flexibility and options, 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 a program that processes a large data set in seconds, hours, or not at all. Spend some time reviewing the different characteristics of the class so that you can make the best choice possible.

This chapter explains how you can use the generic collection classes provided by the System.Collections .Generic namespace to build strongly typed collection classes of several useful types. Chapter 19 explains how you can build generic classes of your own. Using generics, you can build strongly typed classes that manipulate objects in any way you can imagine.




Visual Basic 2005 with  .NET 3.0 Programmer's Reference
Visual Basic 2005 with .NET 3.0 Programmer's Reference
ISBN: 470137053
EAN: N/A
Year: 2007
Pages: 417

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