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.

ArrayList Capacity

As previously mentioned, when an ArrayList is first declared, it is created with a predetermined size. As items are added and the capacity is exceeded, it automatically doubles to allow the new additions. Each time this occurs, there is an overhead in allocation of new memory and copying of ArrayList data. It is advisable, therefore, to set the capacity of an ArrayList when the maximum size of the collection is known and when the number of entries that may be used is reasonably stable.

The capacity of an ArrayList can be obtained and modified using the Capacity property. This holds an integer.

ArrayList myCollection = new ArrayList();

int capacity = myCollection.Capacity;
Console.WriteLine("Default capacity is {0} items", capacity);

myCollection.Capacity = 40;
capacity = myCollection.Capacity;
Console.WriteLine("New capacity is {0} items", capacity);

/* OUTPUT

Default capacity is 16 items
New capacity is 40 items

*/

Once an ArrayList has been fully populated and no further items will be added, the memory allocated to the unused part of the collection can be reclaimed. The TrimToSize method sets the capacity of the collection to match the current contents.

ArrayList myCollection = new ArrayList();

myCollection.Add("One");
myCollection.Add("Two");
myCollection.Add("Three");
myCollection.Add("Four");
myCollection.Add("Five");

int capacity = myCollection.Capacity;
Console.WriteLine("Initial capacity is {0} items", capacity);

myCollection.TrimToSize();
capacity = myCollection.Capacity;
Console.WriteLine("New capacity is {0} items", capacity);

/* OUTPUT

Initial capacity is 16 items
New capacity is 5 items

*/

ArrayList Wrappers

Creating a Fixed Size ArrayList

It is possible to create an ArrayList that has a fixed size using the static FixedSize method. This method is executed against an existing ArrayList to return a new, fixed size version. The fixed size collection is actually a wrapper for the original collection so any changes made to either ArrayList are mirrored exactly in the other. However, any attempt to add a new item to the fixed size variant causes a NotSupportedException to be thrown.

NB: New items may be added to the underlying ArrayList. These will be added to the fixed size collection without an exception occurring.

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

ArrayList myFixed = ArrayList.FixedSize(myCollection);

Console.WriteLine(myFixed.IsFixedSize);     // Outputs "True"

myFixed.Add("Six");                         // Throws a NotSupportedException

Creating a Read-Only ArrayList

In some cases, a fixed size wrapper for an ArrayList is not restrictive enough. Occasionally it is useful to create a wrapper collection that is read-only. This type of ArrayList, generated by the static ReadOnly method, causes a NotSupportedException if any attempt is made to modify its contents. The underlying ArrayList may be modified.

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

ArrayList myReadOnly = ArrayList.ReadOnly(myCollection);

Console.WriteLine(myReadOnly.IsFixedSize);  // Outputs "True"
Console.WriteLine(myReadOnly.IsReadOnly);   // Outputs "True"

myReadOnly[0] = "Apple";                    // Throws a NotSupportedException
5 May 2007