Basic Collections
(Page 1 of 6 )
Programming involves manipulating objects. However, often objects are not isolated. Rather, objects often only make sense in context with other, similar objects. Because of this, it becomes necessary to organize objects. There are two methods of organization: arrays and collections. This article will focus on collections.
Arrays are fine if you know exactly what will be in them at compile-time or if you can determine how many elements they will contain at run-time. If you are unable to make either determination, though, then using arrays becomes problematic. Instead, when it's necessary to organize arbitrary amounts of data at run-time, collections are more suited to the task. The .NET framework contains a good many collections designed for different scenarios. This article will explore these collections.
Lists (And a Discussion of Generic Collections)
First, let's take a look at an array, examining how it functions. There are two ways we can define an array. The first is by providing initial values, and the second is by providing a size. Here, we define two arrays using both methods:
string[] array1 = {"one", "two", "three"};
int[] array2 = new int[3];
We can access individual elements of either array by providing an index. Here, we write the first element of the first array to the console:
Console.WriteLine(array1[0]);
Assignment is done in a similar manner. Here, we re-assign the value of the second element of the first array, and we re-assign the values of each element of the second array:
array1[1] = "one and a half";
array2[0] = 1;
array2[1] = 2;
array2[2] = 3;
Looping through arrays is very easy; so is using the foreach loop. Here, we write each element of the second array to the console:
foreach (int element in array2)
{
Console.WriteLine(element);
}
From all of this, we can safely conclude that arrays are easy enough to use and manipulate. Everything about arrays is fairly straightforward. However, the problem is that arrays have fixed sizes. Both arrays in our example contain three elements – no more and no less because there's no way to change this. For example, suppose I wanted to gather all of the prime numbers less than one hundred thousand. If I made each prime number an element in an array, I would have to know beforehand how many prime numbers are under one hundred thousand. That would be quite inconvenient.
This is where collections come in. Collections contain data just as arrays do, but they offer more functionality (and different functionality) and flexibility. Suppose there was a way to create something similar to an array in behavior except that it would feature a dynamic size. This is exactly what a list does! It works similarly to an array, but you can add and remove elements as needed. A list called ArrayList is located in the System.Collections.List namespace. It will store any number of objects. Here, we define an ArrayList:
ArrayList arrayList1 = new ArrayList();
Notice how we don't specify the size as we do with a regular array. This isn't necessary. In order to add elements into the ArrayList, the Add method is used, which accepts an object. Let's add three elements to our ArrayList:
arrayList1.Add("five");
arrayList1.Add("six");
arrayList1.Add("seven");
Additionally, with .NET 3.0, the initial elements of an ArrayList can be added on the same line as the initialization of the ArrayList itself, as with arrays:
ArrayList arrayList1 = new ArrayList() { "five", "six", "seven" };
Elements in an ArrayList can be accessed by their zero-based indexes, just as in arrays. However, recall that an ArrayList's elements are all of type object. This means that in order to use a given element, it must be cast into its proper type. Here, we extract the first element from arrayList1:
string someString = (string)arrayList1[0];
This, however, looks messy. Moreover, it's also error-prone. For example, an int could be added to our ArrayList (it will be implicitly cast to object):
arrayList1.Add(8);
Next: Lists Continued >>
More ASP.NET Articles
More By Peyton McCullough