System.Collections.ArrayList
Added in .NET 1.1
This allows you to work with a set of values as if they were an array and a collection at the same time.
System.Collections.ArrayList myArrayList;
Represents a collection of items whose size can be changed dynamically.
ArrayList vs Array
An arraylist is a combination of an array and a collection.
Imagine that you need to keep track of various objects but you have no idea how many.
It is difficult to use an array for this purpose because you must declare the size of the array at compile time.
This is an aray whose size is dynamically increased as required.
Features
An arraylist has the following functionality:
You can address a value by its index
You can quickly sort and reverse the list
You can search sequentially
You can append elements to the end and insert them in a particular position
Different Data Types
These items can have different data types.
System.Collections.ArrayList myArrayList;
myArrayList = new System.Collections.ArrayList();
myArrayList.Add("string");
myArrayList.Add(20);
myArrayList.Add(false);
Console.WriteLine(myArrayList[1]);
Do not return null ?
return new System.Collections.ArrayList
Methods
| Add | Appends an element after the last element in the collection and returns the index where it was inserted. |
| AddRange | |
| BinarySearch | |
| Clear | Removes all elements from the collection. |
| Clone | |
| Contains | Returns TRUE if an element exists in the collection. |
| CopyTo | Copies the elements from the collection to a new one-dimensional array, starting at the specified index in the array. |
| FixedSize | |
| GetEnumerator | Returns an enumerator to iterate an ArrayList |
| GetRange | |
| IndexOf | Returns the index of the element in the collection, or -1 if it cannot be found. |
| Insert | Inserts an element at the specified index. |
| InsertRange | |
| LastIndexOf | |
| ReadOnly | |
| Remove | Removes an element from the collection |
| RemoveAt | Removes an element at the specified index from the collection |
| RemoveRange | |
| Repeat | |
| Reverse | Reverses the order of the elements |
| SetRange | |
| Sort | Alphabetically sorts the elements |
| ToArray | Copies all the elements to a new one-dimensional array |
| TrimToSize |
Properties
| Capacity | Property contains the number of elements the srray can currently hold. |
| Count | Returns the number of elements in the collection. |
| IsFixedSize | Returns TRUE if no more elements can be added to the collection. |
| IsReadOnly | Returns TRUE is the elements cannot be altered. |
Generic Version
The generic version of this collection is System.Collections.Generic.List
Adding
Initailising ArrayLists
The default capacity for a new arraylist is 16 elements.
System.Collections.ArrayList myArrayList;
myArrayList = new System.Collections.ArrayList();
Accessing Elements - Zero Based
Items can be returned using an index that starts at zero.
System.Collections.ArrayList myArrayList;
myArrayList = new System.Collections.ArrayList();
myArrayList.Add("string");
myArrayList.Add(20);
myArrayList.Add(false);
Console.WriteLine(myArrayList[1]);
Capacity
You should always try and define an initial capacity for your arraylist if possible.
You can modify the capacity at any time by assigning a value to the Capacity property
myArrayList.Capacity = myArrayList.Count
Another way to achieve the same result is to use the TrimToSize property
myArrayList.TrimToSize
When the current capacity is exceeded the ArrayList will automatically double its capacity.
Inserting
Insert - Inserts an element at the specified index.
InsertRange -
Removing
There are two ways you can remove all the instances of an element.
Remove
The Remove method will only remove the first occurrence of an element.
myArrayList.Remove("string");
while (myArrayList.IndexOf("remove this") >= 0)
{
myArrayList.Remove("remove this");
}
RemoveAt
myArrayList.RemoveAt(1);
Also this method does not generate an exception if the element you want to remove cannot be found.
RemoveRange
Finding
System.Collections.ArrayList myArrayList = new System.Collections.ArrayList();
myArrayList.Add("GBP");
myArrayList.Add("USD");
myArrayList.Add("EUR");
Console.WriteLine(myArrayList[1]);
Contains
Does an Element Exist
if (myArrayList.Contains(sElement) == true)
{
}
IndexOf
Returns the index of the element in the collection, or -1 if it cannot be found.
LastIndexOf
Updating
Looping
for
for (int icount = 0; icount < myArrayList.Count; icount++)
{
System.Console.WriteLine(myArrayList[icount]);
}
for - each
foreach (string element in myArrayList)
{
System.Console.WriteLine(element);
}
while
do - while
This loops until the count property becomes constant.
int isavecount;
do
{
isavecount = alsArrayListStrings.Count;
myArrayList.Remove("remove this");
} while (alsArrayListStrings.Count < isavecount)
Converting
Converting to a String Array
string[] arValues;
System.Collections.ArrayList myArrayList;
arValues = (string[])arArrayList.ToArray(GetType(String))
© 2026 Better Solutions Limited. All Rights Reserved. © 2026 Better Solutions Limited TopPrevNext