What Are Collections?We'd like a way to gather up objects as they are created so that we can manage them as a group and operate on them collectively, along with referring to them individually when necessary. For example:
We use a special type of object called a collection to group other objects. A collection object can hold/contain multiple references to some other type of object. Think of a collection like an egg carton, and the objects it holds like the eggs! Both are objects, but with decidedly different properties. Because collections are implemented as objects, this implies that.
Let's discuss each of these three matters in turn.Collections Must Be Instantiated Before They Can First Be UsedWe can't merely declare a collection:CollectionType c;For example:ArrayList c;All this does is to declare a reference variable of type CollectionType. Until we "hand"c a CollectionType object to refer to, c is said to have the value null.
We have to take the distinct step of using the new operator to actually create an empty CollectionType object in memory, as follows:c = new CollectionType();For example:c = new ArrayList();Think of the newly created CollectionType object as an empty "egg carton", and the reference variable c as the handle that allows us to locate and access (reference) this egg carton whenever we'd like.Then, as we instantiate objects ("eggs"), we'll place their references into the various egg carton compartments: Student s = new Student(); // Pseudocode. c.Add(s);So, rather than thinking of the objects as eggs that are physically placed inside of the egg carton compartments, we should really think of the objects as balloons whose strings are tied inside the egg carton compartments, as illustrated in Figure 6-1. That is, the objects themselves live physically outside of the collection, but can be located through their references, which are stored within the collection.Figure 6-1. A collection organizes object references.Thus, perhaps a more appropriate analogy than a collection as an egg carton would be that of a collection as an address book: we make an entry in the address book (collection) for each of the persons (objects) that we wish to contact, but the actual persons themselves are physically remote (see Figure 6-2).Figure 6-2. A collection references objects, which live separately in memory.
Collections Are Defined by ClassesHere is a code snippet that illustrates the use of a collection in C#; we use a bit of pseudo code here to emphasize the common features of collections.// Instantiate a collection object (pseudocode).CollectionType x = new CollectionType();
// Create a few Student objects.Student a = new Student();Student b = new Student();Student c = new Student();
// Store all three students in the collection by calling the appropriate// method for adding objects to the collection ...x.Add(a);x.Add(b);x.Add(c);// ... and then retrieve the first one.x.Retrieve(0); // we typically start counting at 0.OO Collections Are EncapsulatedWe don't need to know the private details of how object references are stored internally to a specific type of collection in order to use the collection properly; we only need to know a collection's public features-in particular, its method headers and properties-in order to choose an appropriate collection type for a particular situation and to use it effectively.
Virtually all collections, regardless of type and regardless of the programming language in which they are implemented, provide, at a minimum, methods for Adding object (reference)s Removing object (reference)s Retrieving specific individual object (reference)s Iterating through the object (reference)s in some predetermined order Getting a count of the number of object (reference)s in the container Answering a true/false question as to whether a particular object is referenced by the container or not Throughout this chapter, we'll talk casually about manipulating objects in collections, but please remember that, with C#, what we really mean is that we're manipulating object references.
Throughout this chapter, we'll talk casually about manipulating objects in collections, but please remember that, with C#, what we really mean is that we're manipulating object references.