• Home
  • Testing
  • SAP
  • Web
  • Must Learn!
  • Big Data
  • Live Projects
  • Blog
  • Job

What is ArrayList in C#?

The ArrayList collection is similar to the Arrays data type in C#. The biggest difference is the dynamic nature of the array list collection.

For arrays, you need to define the number of elements that the array can hold at the time of array declaration. But in the case of the Array List collection, this does not need to be done beforehand. Elements can be added or removed from the Array List collection at any point in time. Let's look at the operations available for the array list collection in more detail.

Declaration of an Array List

The declaration of an ArrayList is provided below. An array list is created with the help of the ArrayList Datatype. The "new" keyword is used to create an object of an ArrayList. The object is then assigned to the variable a1. So now the variable a1 will be used to access the different elements of the array list.

ArrayList a1 = new ArrayList()

Adding elements to an array

The add method is used to add an element to the ArrayList. The add method can be used to add any sort of data type element to the array list. So you can add an Integer, or a string, or even a Boolean value to the array list. The general syntax of the addition method is given below

ArrayList.add(element)

Below are some examples of how the "add" method can be used. The add method can be used to add various data types to the Array List collection.

Below you can see examples of how we can add Integer's Strings and even Boolean values to the Array List collection.

  • a1.add(1) – This will add an Integer value to the collection
  • a1.add("Example") – This will add a String value to the collection
  • a1.add(true) – This will add a Boolean value to the collection

Now let's see this working at a code level. All of the below-mentioned code will be written to our Console application. The code will be written to our Program.cs file.

In the program below, we will write the code to create a new array list. We will also show to add elements and to display the elements of the Array list.

C# Collections

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DemoApplication
{
 class Program
 {
  static void Main(string[] args)
  {
   ArrayList a1 = new ArrayList();
   a1.Add(1);
   a1.Add("Example");
   a1.Add(true);
   
   Console.WriteLine(a1.Count);
   Console.WriteLine(a1.Contains(2));
   Console.WriteLine(a1[1]);
   a1.RemoveAt(1);
   Console.WriteLine(a1[1]);
   Console.ReadKey();
  }
 }
}

Code Explanation:-

  1. The first step is used to declare our Array List. Here we are declaring a1 as a variable to hold the elements of our array list.
  2. We then use the add keyword to add the number 1 , the String "Example" and the Boolean value 'true' to the array list.
  3. We then use the Console.WriteLine method to display the value of each array lists element to the console. You will notice that just like arrays, we can access the elements via their index positions. So to access the first position of the Array List, we use the [0] index position. And so on and so forth.

If the above code is entered properly and the program is run the following output will be displayed.

Output:

C# Collections

From the output, you can see that all of the elements from the array list are sent to the console.

Let's look at some more methods which are available as part of the ArrayList.

Count

This method is used to get the number of items in the ArrayList collection. Below is the general syntax of this statement.

ArrayList.Count() – This method will return the number of elements that the array list contains.

Contains

This method is used to see if an element is present in the ArrayList collection. Below is the general syntax of this statement

ArrayList.Contains(element) – This method will return true if the element is present in the list, else it will return false.

RemoveAt

This method is used to remove an element at a specific position in the ArrayList collection. Below is the general syntax of this statement

ArrayList.RemoveAt(index) – This method will remove an element from a specific position of the Array List.

Now let's see this working at a code level. All of the below-mentioned code will be written to our Console application. The code will be written to our Program.cs file.

In the below program, we will write the code to see how we can use the above-mentioned methods.

C# Collections

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DemoApplication
{
 class Program
 {
  static void Main(string[] args)
  {
   ArrayList al = new ArrayList();
   al.Add(1);
   al.Add("Example");
   al.Add(true);
   
   Console.WriteLine(a1.Count);
   Console.WriteLine(a1.Contains(2));
   Console.WriteLine(a1[1]);
   a1.RemoveAt(1);
   Console.WriteLine(a1[1]);
   Console.ReadKey();
  }
 }
}

Code Explanation:-

  1. So the first property we are seeing is the Count property. We are getting the Count property of the array list a1 and then writing it to the Console.
  2. In the second part, we are using the Contains method to see if the ArrayList a1 contains the element 2. We then write the result to the Console via the Writeline command.
  3. Finally, to showcase the Remove element method, we are performing the below steps,
    1. First, we write the value of the element at Index position 1 of the array list to the console.
    2. Then we remove the element at Index position 1 of the array list.
    3. Finally, we again write the value of the element at Index position 1 of the array list to the console. This set of steps will give a fair idea whether the remove method will work as it should be.

If the above code is entered properly and the program is run the following output will be displayed.

Output:

C# Collections

Why is the last value true?

If you see the sequence of events, the element Example is removed from the array because this is at position 1. Position 1 of the array then gets replaced by what was in position 2 earlier which the value 'true'

Summary

  • The Array List collection is used to store a group of elements. The advantage of the Array list collection is that it is dynamic. You can add and remove elements on the fly to the array list collection.

 

YOU MIGHT LIKE: