Arrays C# Collection

How to Create an ArrayList in C#

In C#, an ArrayList stores elements of multiple data types whose size can be changed dynamically. 

To create ArrayList in C#, we need to use the System.Collections namespace. Here is how we can create an arraylist in C#.

Here, we have created an arraylist named myList.

Basic Operations on ArrayList

In C#, we can perform different operations on arraylists. We will look at some commonly used arraylist operations in this tutorial:

  • Add Elements
  • Access Elements
  • Change Elements
  • Remove Elements

Let’s see how we can perform these operations in detail!

Add Elements in ArrayList

C# provides a method Add() using which we can add elements in ArrayList. For example,

In the above example, we have created an ArrayList named student.

Then we added "Tina" and 5 to the ArrayList using the Add() method.

Other way to add Elements to ArrayList

Add Elements in an ArrayList using Object Initializer Syntax.

Add Elements in an ArrayList at specified index

To add an element to a specified index in ArrayList, we use the Insert() method. For example,

In the above example, we have inserted "Tim" at the second index position using the Insert() method.

Access ArrayList Elements

We use indexes to access elements in ArrayList. The indexing starts from 0. For example,

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.