BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

.NET Framework
.NET 1.1+

The ArrayList Collection

The thirty-seventh part of the C# Fundamentals tutorial describes the use of the ArrayList class. This general-purpose collection class provides the ability to create and manipulate a group of related objects as if they were a variable-length array.

Copying an ArrayList

Copying to an Array

As the ArrayList class implements the IList interface, it supports the use of the CopyTo method to copy the contents of a collection into a one-dimensional array. In addition, the ArrayList provides two overloaded versions of the method. The first requires no parameters and simply copies to entire contents of the collection into an existing array. The array must be compatible with the types of object in the ArrayList to avoid an exception.

ArrayList myCollection = new ArrayList(
    new string[] {"One", "Two", "Three", "Four", "Five"});

string[] array = new string[5];

// Copy items
myCollection.CopyTo(array);

// List array items
foreach (string s in array)
{
    Console.WriteLine(s);
}

/* OUTPUT

One
Two
Three
Four
Five

*/

The second overloaded CopyTo method requires four parameters. The first specifies the zero-based index in the ArrayList being copying from. The second is the ArrayList object itself. The third holds the first index in the target array to receive copied items and the final parameter determines how many entries from the collection should be copied. This allows you to copy a subset of data from the ArrayList into an existing array.

ArrayList myCollection = new ArrayList(
    new string[] {"One", "Two", "Three", "Four", "Five"});

string[] array = new string[5];

// Copy items
myCollection.CopyTo(1,array,0,3);

// List array items
foreach (string s in array)
{
    Console.WriteLine(s);
}

/* OUTPUT

Two
Three
Four

*/

Another function exists that permits an ArrayList's contents to be extracted into an array. The ToArray method returns a new array of objects. This means that the array does not need to be created prior to the copy. However, the basic method only generates an array of object instances. In the following example the ArrayList items are copied to a series of objects; each a boxed string.

ArrayList myCollection = new ArrayList(
    new string[] {"One", "Two", "Three", "Four", "Five"});

// Copy items
object[] array = myCollection.ToArray();

// List array items
foreach (object o in array)
{
    Console.WriteLine(s);
}

/* OUTPUT

One
Two
Three
Four
Five

*/

If the ArrayList contains a collection of items that are all of the same type, this type may be specified when using ToArray. If every item in the collection can be successfully cast to the specified type then the copying creates a new array of that type. To indicate the type to the compiler, the typeof command is used. This command accepts the relevant class or data type name as a parameter. It is important to note that the method still returns an array of objects. However, because the types of the array elements are consistent, the entire array may then be cast to the correct type.

In the following example, note the use of typeof(string) to specify the type of each array element to copy. Also note the casting of the object array to a string array with the cast operator (string[]).

ArrayList myCollection = new ArrayList(
    new string[] {"One", "Two", "Three", "Four", "Five"});

// Copy items
string[] array = (string[])myCollection.ToArray(typeof(string));

// List array items
foreach (string s in array)
{
    Console.WriteLine(s);
}

/* OUTPUT

One
Two
Three
Four
Five

*/
5 May 2007