Chapter 3. Storing Objects in Specialized Collections

In This Chapter

Looping through a collection

Storing data in the ArrayList class

Associating data with a Hashtable

Stacking and queuing your data

Storing collections of strings

Sorting associations with an OrderedDictionary

A collection is a common name for a class that lets you store instances of other classes. Collections vary in design; some let you store only instances of a particular class; others let you store instances of any class. Some collections hold what are called associations, where each item in the collection associates groups of two objects, such as cars and colors.

If you’re familiar with arrays in .NET programming, then you’re already familiar with collections. As with arrays, the collection classes in .NET let you put items in the collection, find out what’s in the collection, and iterate through the collection. (The word iterate simply means loop through the collection again and again, discovering individual elements one by one.)

One of the limitations of .NET collection classes is that they are not type-specific. In other words, they can hold objects of any class. Version 2.0 of the .NET Framework introduces a new type of collection called a generic collection, which can be type-specific. In this chapter, I introduce you to plain-vanilla collections (that is, non-generic collections). Then, in Chapter 4 of this mini-book, you’ll learn how to use type-specific generic collections.

Iterating Through a Collection ...

Get ASP.NET 2.0 All-In-One Desk Reference For Dummies® now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.