ArrayList class in C#

How to use the ArrayList class in C#?

In this article, let us see the uses of ArrayList class in C#.

After reading this article, you will learn about,

  • ArrayList
  • Uses of ArrayList
  • How to Access elements in ArrayList
  • Types of used in Methods in ArrayList
How to use the ArrayList class in C#? #arraylist #csharp #programming Click To Tweet

First and foremost thing we have to remember about an 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 solve 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 the 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 from the list, we must do some Conversions.

List of Methods Used in Array List

Add:

Add an Object to the end of the ArrayList.

Remove:

Remove the first appeared object from the ArrayList.

Clear:

Removes all elements from the ArrayList.

Insert:

Inserts an element into the ArrayList at the specified index.

TrimToSize:

Sets the Capacity to the actual number of elements in the ArrayList.

Sort:

Sorts the elements in the ArrayList.

Reverse:

Reverse the elements in the ArrayList.

The following sample code show how to use an ArrayList,

We must include the system.collection namespace as shown below

using System;

using System.Collections;

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
    {
        get
        {
            return theAnimals;
        }
    }

    public Animal this[int i]
    {
        get
        {
            return (Animal)theAnimals[i];
        }

        set
        {
            theAnimals[i] = value;
        }
    }

    public Zoo()
    {
        theAnimals = new ArrayList();
    }
}

The Add and Insert methods are used to add elements to the array. This is main difference between Array and ArrayList.

public class Guardian
{
    static void Main(string[] args)
    {
        Zoo myzoo = new Zoo();

        myzoo.ZooAnimals.Add(new Lion());

        myzoo.ZooAnimals.Add(new Elephant());

        myzoo.ZooAnimals.Insert(1, new Lion());

        Animal a = myzoo[0];

        myzoo[1] = new Tiger();
    }
}

We can access the elements of the ArrayList by using the Index Operator[] as below:

Animal a = myzoo[0];
myzoo [1] = new Tiger();

I hope, this article was helpful to you. You can also refer to the post on non-generic collections in C#.

Thanks for reading and let me know your valuable comments if any.

Like this post? Don’t forget to share it!

This is a guest post by Prem Kumar. If you would like to write for DotnetCrunch, check our guest posting guidelines.

Was this article worth reading? Share it with fellow developers too.

Prem Kumar

I am a Technical Specialist with 4+ years of experience in developing Web-based enterprise applications with special interest in server side services, tools/libraries and frameworks.