C# - The ArrayList Class

The ArrayList class provides a number of properties and methods that are used to work with an arraylist. Arraylists are zero-based in ASP.NET.

Properties and Methods of the ArrayList Class

Item Description
Properties
Capacity Contains the allocated length of the arraylist
Count Contains the number of items currently in the arraylist
IsFixedSize Contains True if the arraylist is of a fixed size
IsReadOnly Contains True if the arraylist is read-only and False if it is not read-only
IsSynchronized Contains True if the arraylist is synchronized
Item[] Stores an index of all the items in the arraylist
Methods
Adapter() Accepts a list and returns an arraylist
Add() Adds an item to the arraylist and returns the newly added index
AddRange() Adds a collection to the arraylist and returns the index of the first item added
BinarySearch() Performs a binary search on the arraylist
Clear() Clears all items from the arraylist
Clone() Creates a duplicate of the arraylist
Contains() Returns True if the given object is in the current arraylist
CopyTo() Copies all or part of the current arraylist to another arraylist that is passed in as an argument
FixedSize() Returns an arraylist of a fixed size
GetRange() Returns the range of the current arraylist
IndexOf() Returns the index of an item in the arraylist
Insert() Inserts an item into the arraylist
InsertRange() Inserts a collection of items into the arraylist
LastIndexOf() Returns the last index of an item in the arraylist
ReadOnly() Returns a read-only copy of the current arraylist
Remove() Removes an item from the arraylist
RemoveAt() Removes an item from the arraylist by index
RemoveRange() Removes a range of items from the arraylist
Repeat() Creates an arraylist of specified size, with all elements containing the same object
Reverse() Reverses items in the arraylist
SetRange() Sets the range of the arraylist
Sort() Sorts the items in the arraylist
Synchronized() Synchronizes the arraylist
ToArray() Converts the arraylist to an array of the type passed in as an argument; returns the new array
TrimToSize() Changes the size of the arraylist to match the number of items currently in the arraylist
ArrayList.Capacity

Syntax

Int32 Capacity

Description

The Capacity property allows the size of the arraylist to be retrieved and set. If you attempt to set the size of the arraylist so that it is smaller than the current size of the arraylist, an exception is thrown.

Example

ArrayList items = new ArrayList();
items.Add("item1");
items.Add("item2");
items.Add("item3");

Items.Capacity = 5; //The default size is 16

MyLabel.Text = "Capacity is "+items.Capacity.ToString();
ArrayList.Count

Syntax

Int32.Count

Description

The Count property contains the number of items in the arraylist.

Example

ArrayList items = new ArrayList();
items.Add("item1");
items.Add("item2");
items.Add("item3");

MyLabel.Text = "Count is "+items.Count.ToString();
ArrayList.IsFixedSize

Syntax

Boolean IsFixedSize

Description

The IsFixedSize property contains True if the arraylist is of a fixed size. This property is set to False by default.

Example

ArrayList items = new ArrayList();
items.Add("item1");
items.Add("item2");
items.Add("item3");

MyLabel.Text = IsFixedSize set to "+items.isFixedSize;
ArrayList.IsReadOnly

Syntax

Boolean IsReadOnly

Description

The IsReadOnly property contains True if the arraylist is read-only. This property is set to False by default. To change the read-only state of an arraylist, you use the ArrayList.ReadOnly() method.

Example

ArrayList items = new ArrayList();
items.Add("item1");
items.Add("item2");
items.Add("item3");

MyLabel.Text = "IsReadOnly set to "+items.IsReadOnly;
ArrayList.IsSynchronized

Syntax

Boolean IsSynchronized

Description

The IsSynchronized property contains True if the arraylist is synchronized and False otherwise.

Example

ArrayList items = new ArrayList();
items.Add("item1");
items.Add("item2");
items.Add("item3");

MyLabel.Text = "IsSynchronized set to "+items.IsSynchronized;
ArrayList.Item

Syntax

Object Item ( Int32 index )

Description

The Item property enables a developer to access elements of the arraylist by using a numerical index. Attempts to access an element outside the current range of the arraylist cause an exception to be thrown.

Example

ArrayList items = new ArrayList();
items.Item[0] = "item1";

MyLabel.Text = "First Item in ArrayList: "+(string)items[1];
ArrayList.Adapter()

Syntax

ArrayList Adapter ( IList list )

Description

The Adapter() method is used to convert any list into an Item[] property. It accepts an IList object as an argument and returns an arraylist.

Example

ArrayList items = new ArrayList();
items.Add("item1");
items.Add("item2");
items.Add("item3");

ArrayList NewList = new ArrayList();
NewList = ArrayList.Adapter(items);
ArrayList.Add()

Syntax

Int32 Add ( Object value )

Description

The ArrayList.Add() method is used to add an element to an arraylist. It automatically increases the size of the arraylist if needed, as long as the arraylist is not a fixed size.

Example

ArrayList items = new ArrayList();
items.Add("item1");
items.Add("item2");
ArrayList.AddRange()

Syntax

void AddRange( ICollection c )

Description

ArrayList.AddRange() accepts a collection and adds the items in the collection to the arraylist. Like the ArrayList.Add() method, ArrayList.AddRange automatically expands the capacity of the arraylist unless the arraylist is of fixed size.

Example

ArrayList items = new ArrayList();
items.Add("item1");
items.Add("item2");

ArrayList NewList = new ArrayList();
NewList.Add("newitem1");
NewList.Add("newitem2");

items.AddRange(NewList);
ArrayList.BinarySearch()

Syntax

Int32 BinarySearch( Object value, IComparer comparer )
Int32 BinarySearch( Object value )
Int32 BinarySearch( Int32 index, Int32 count, Object value, IComparer comparer)

Description

The BinarySearch() method performs a binary search on the given arraylist. The simplest method for performing a binary search on an arraylist is to pass in a search value only. This will search the entire arraylist for the value. Passing in a comparer as the second argument enables the developer to search more specifically. Finally, for large arraylists, passing in a starting index and range as the first two arguments will greatly reduce search time.

Example

MyLabel.Text = items.BinarySearch("item2").ToString();
ArrayList.Clear()

Syntax

void Clear()

Description

The ArrayList.Clear() method removes all items from the arraylist collection.

Example

ArrayList items = new ArrayList();
items.Add("item1");
items.Clear();
ArrayList.Clone()

Syntax

Object Clone()

Description

The ArrayList.Clone() method creates a copy of the given arraylist. It returns a generic Object type of arraylist.

Example

ArrayList items = new ArrayList();
items.Add("item1");

ArrayList NewList = (ArrayList)items.Clone();
ArrayList.Contains()

Syntax

Boolean Contains( Object item )

Description

The ArrayList.Contains() method accepts a search object as an argument and returns True if the object is found in the arraylist.

Example

ArrayList items = new ArrayList();
items.Add("item1");

MyLabel.Text = "Does list contain this object: "+items.Contains("item1").ToString();
ArrayList.CopyTo()

Syntax

void CopyTo( Array array, Int32 arrayIndex )
void CopyTo( Array array )
void CopyTo( Int32 index, Array array, Int32 arrayIndex, Int32 count )

Description

The ArrayList.CopyTo() method is used to copy the contents of an arraylist to an array. The simplest way to do this is to pass the array as the only argument. This copies the entire arraylist to the array. The array must be large enough to hold all the arraylist elements, or an exception will be thrown.

Example

ArrayList items = new ArrayList();
items.Add("item1");
items.Add("item2");

String[] MyArray = new String[2];
items.CopyTo(MyArray);

MyLabel.Text = "First item of ArrayList is "+MyArray[0];
ArrayList.FixedSize()

Syntax

IList FixedSize( IList list )
ArrayList FixedSize(ArrayList list )

Description

The ArrayList.FixedSize() method accepts either a list or an arraylist as an argument and returns a fixed-size version of the list or arraylist.

Example

ArrayList items = new ArrayList();
items.Add("item1");
items.Add("item2");

ArrayList NewList = new ArrayList();
NewList = ArrayList.FixedSize(items);

// the following line will throw exception because item3 cannot be added due to fixed-size is set to 2 items
NewList.Add("item3"); 
ArrayList.GetRange()

Syntax

ArrayList GetRange( Int32 index, Int32 count )

Description

The ArrayList.GetRange() method accepts an index and a range (as count). It returns an arraylist of the original arraylist, starting at the given index and range.

Example

ArrayList items = new ArrayList();
items.Add("item1");
items.Add("item2");

ArrayList NewList = new ArrayList();
NewList = items.GetRange(1, 2);
ArrayList.IndexOf()

Syntax

Int32 IndexOf( Object value )
Int32 IndexOf( Object value, Int32 startIndex )
Int32 IndexOf( Object value, Int32 startIndex, Int32 endIndex )

Description

The ArrayList.IndexOf() method is used to find an index of the given value in the arraylist.

There are two overloaded IndexOf() methods. The first enables the developer to enter a starting index, and the second enables the developer to specify both a starting and ending index. In large arraylists, passing in these extra arguments could greatly reduce search time.

If the value is not found in the arraylist, ArrayList.IndexOf() return -1. If the value is in the arraylist more than once, ArrayList.IndexOf() returns the index of the first instance.

Example

ArrayList items = new ArrayList();
items.Add("item1");
items.Add("item2");

MyLabel.Text = "IndexOf 'item2' is: "+items.IndexOf("item2");
ArrayList.Insert()

Syntax

Void Insert( Int32 index, Object value )

Description

The ArrayList.Insert() method is used to insert a value into an arraylist at a given index. The size of the original arraylist will be increased and reindexed automatically.

Example

ArrayList items = new ArrayList();
items.Add("item1");
items.Add("item2");

items.Insert(1, "InsertedValue");
ArrayList.InsertRange()

Syntax

Void InsertRange( Int32 index, ICollection c )

Description

The ArrayList.InsertRange() method inserts a collection of items into an arraylist at the given index.

Example

ArrayList items = new ArrayList();
items.Add("item1");
items.Add("item2");

ArrayList NewList = new ArrayList();
NewList.Add("NewValue1");
NewList.Add("NewValue2");

items.InsertRange(1, NewList);
ArrayList.LastIndexOf()

Syntax

Int32 LastIndexOf( Object value )
Int32 LastIndexOf( Object value, Int32 startIndex )
Int32 LastIndexOf( Object value, Int32 startIndex, Int32 endIndex)

Description

The ArrayList.LastIndexOf() method returns the last index of the value in the arraylist. Overloaded methods of LastIndexOf() enable the developer to pass in a start index and an end index to limit the range and increase search performance.

Example

ArrayList items = new ArrayList();
items.Add("item1");
items.Add("item2");
items.Add("item3");

MyLabel.Text = "Last Index of 'item1' is: " + items.LastIndexOf("item1");
ArrayList.ReadOnly()

Syntax

IList ReadOnly( IList list )
ArrayList ReadOnly( ArrayList list )

Description

The ArrayList.ReadOnly() method accepts either an IList object or an arraylist as an argument and returns a read-only copy of the same type.

Example

ArrayList items = new ArrayList();
items.Add("item1");
items.Add("item2");

ArrayList NewList = ArrayList.ReadOnly(items);

//the following line will throws an error because the 'items' is set to read-only status
NewList.Add("item3");
ArrayList.Remove()

Syntax

Void Remove( Object obj )

Description

The ArrayList.Remove() method removes the first instance of an object found in an arraylist. An exception is thrown if the object is not found in the arraylist.

Example

ArrayList items = new ArrayList();
items.Add("item1");
items.Add("item2");

items.Remove("item1");
ArrayList.RemoveAt()

Syntax

Void RemoveAt( Int32 index )

Description

The ArrayList.RemoveAt() method removes an item from an arraylist by index passed in as an argument.

Example

ArrayList items = new ArrayList();
items.Add("item1");
items.Add("item2");

items.RemoveAt(0);
ArrayList.RemoveRange()

Syntax

Void RemoveRange( Int32 index, Int32 count )

Description

The ArrayList.RemoveRange() method removes a range of items from an arraylist.

Example

ArrayList items = new ArrayList();
items.Add("item1");
items.Add("item2");

items.RemoveRange(0, 2);
ArrayList.Repeat()

Syntax

ArrayList Repeat( Object value, Int32 count )

Description

The ArrayList.Repeat() method returns an arraylist of size Count (second argument), which contains the value passed in as the first argument.

Example

ArrayList NewList = new ArrayList();
NewList = ArrayList.Repeat("aItem", 5); //return an arraylist with 5 items, all containing the value 'aItem'
ArrayList.Reverse()

Syntax

Void Reverse()
Void Reverse( Int32 index, Int32 count )

Description

The ArrayList.Reverse() method reverses the order of all items in an array. The overloaded Reverse() method enables a developer to pass in an index and count to reverse only, a subset of the original arraylist.

Example

ArrayList items = new ArrayList();
items.Add("item1");
items.Add("item2");

items.Reverse(); //reverse the order of the items
ArrayList.SetRange()

Syntax

Void SetRange( Int32 index, ICollection c )

Description

The ArrayList.SetRange() method sets the range of a given arraylist to that of a collection passed in as an argument.

Example

ArrayList items = new ArrayList();
items.Add("item1");
items.Add("item2");

ArrayList NewList = new ArrayList();
NewList.Add("newitem1");
NewList.Add("newitem2");

items.SetRange(1, NewList);
ArrayList.Sort()

Syntax

Void Sort()
Void Sort( IComparer comparer )
Void Sort( Int32 index, Int32 count, IComparer comparer )

Description

The ArrayList.Sort() method enables a developer to sort all or part of an arraylist. The simplest Sort() method sorts the entire arraylist based on the list values. The first overloaded method enables the developer to pass in a comparer to better control how the sort is performed. The comparer must be of type IComparer.

The last overloaded method enables a developer to pass in an index and count in addition to the comparer in order to sort only a subset of the items in an arraylist.

Example

ArrayList NewList = new ArrayList();
NewList.Add(2);
NewList.Add(1);
NewList.Add(3);


NewList.Sort();
ArrayList.Synchronized()

Syntax

ArrayList Synchronized( ArrayList list )
IList Synchronized( IList list )

Description

The ArrayList.Synchronized() method accepts either an arraylist or IList object and returns a synchronized list of the same type.

Example

ArrayList items = new ArrayList();
items.Add("item1");
items.Add("item2");

items = ArrayList.Synchronized(items);
ArrayList.ToArray()

Syntax

Object[] ToArray()
Array ToArray( Type type )

Description

The ArrayList.ToArray() method returns an object array back to the caller, with items mirroring those in the original arraylist. If the type is passed into the call to ToArray(), then an array of that type is returned.

Example

Object[] MyArray = items.ToArray();
ArrayList.TrimToSize()

Syntax

Void TrimToSize()

Description

The ArrayList.TrimToSize() method sets ArrayList.Capacity to the number of elements in the arraylist.

Example

items.TrimToSize();