In this article, let see uses of Array list classes in C#.
After reading this article, you will learn about,
2) Uses of ArrayList
3) How to Access elements in ArrayList
4) Types of used in Methods in ArrayList
First and foremost thing have to remember about array is, it does not have a fixed size; it grows as needed always.
Use Add (object) to add an object to the end of the Array List.
Use  to access the elements in the ArrayList.
Use TrimToSize() to reduce the size to fit the number of elements in the Array List.
User Clear to remove all the elements.
Actually, ArrayList can solves the main disadvantage of an array, which is that we must know the capacity of the data structure when u instantiate the array by providing a data structure that behaves like an array but can grow as required.
As elements are added to an ArrayList object, the capacity is automatically increased. An ArrayList object initially allocates 16 elements. When we add a seventeenth element, the ArrayList expands to 32 elements.
How to Access Elements in an ArrayList
We can access elements in an ArrayList object in the same way that you access arrays. We can also use ArrayList methods to add elements to or remove elements from an ArrayList.
To decrease the capacity of an ArrayList, we can call TrimToSize method.
Use Add method to add items to an ArrayList.
We can also do Foreach to iterate over items in an ArrayList.
ArrayList elements are objects, such as System. Object. So when we retrieve the elements form the list, we must do some Conversions.
List of Methods Used in Array List
Add an Object to the end of the ArrayList.
Remove the first appeared object from the ArrayList.
Removes all elements from the ArrayList.
Inserts an element into the ArrayList at the specified index.
Sets the Capacity to the actual number of elements in the ArrayList.
Sorts the elements in the ArrayList.
Reverse the elements in the ArrayList.
The following sample code show how to use an ArrayList,
1) We must include the system.collection namespace as shown below
2) The ArrayList is initialized without specifying the size, because it may grow when needed.
Name of Arraylist is theAnimals as shown below,
public class Zoo
private ArrayList theAnimals;
public ArrayList ZooAnimals
public Animal this[int i]
return (Animal) theAnimals[i];
theAnimals[i] = value;
theAnimals = new ArrayList();
3) The Add and Insert methods are used to add elements to the array. This is main difference between Array and ArrayList, as below
public class Guardian
static void Main(string args)
Zoo myzoo = new Zoo();
myzoo.ZooAnimals.Insert(1, new Lion());
Animal a = myzoo ;
myzoo = new Tiger();
4) We can access the elements of the ArrayList by using the Index Operator as below,
Animal a = myzoo;
myzoo  = new Tiger();
Thanks for reading my article, Hope you like it J