C# ArrayList - Add() Method



The C# ArrayList add() method adds an object to the end of the array list. When the object is added to the ArrayList, this method returns the index at which the value is added.

Syntax

Following is the syntax of the C# ArrayList Add() method −

public virtual int Add (object? value);

Parameters

This method accepts a single parameter which is a value that represents the object to be added to the end of the array −

Return value

This method returns the index of the ArrayList where the value has been added.

Example 1: Add Integer in ArrayList

This is the basic example of the ArrayList Add() method. We can use this method to add object in the arrayList −

    
using System;
using System.Collections;

public class SamplesArrayList {
   public static void Main() {
      // Creates and initializes a new ArrayList.
      ArrayList myArrayList = new ArrayList();
   
      // use the add method
      myArrayList.Add(1);
      myArrayList.Add(2);
      myArrayList.Add(3);
      myArrayList.Add(4);
   
      // Displays the ArrayList.
      Console.WriteLine("The ArrayList contains the following:");
      PrintValues(myArrayList, ' ');
   }
   
   public static void PrintValues(IEnumerable myList, char mySeparator) {
      foreach(object obj in myList) {
         Console.Write("{0}{1}", mySeparator, obj);
      }
      Console.WriteLine();
   }
}

Output

Following is the output −

The ArrayList contains the following:
 1 2 3 4

Example 2: Add Strings in The ArrayList

Let us see another example of the Add() method of the ArrayList to add the string value in the ArrayList −

using System;
using System.Collections;
public class SamplesArrayList {
   public static void Main() {
      // Creates and initializes a new ArrayList.
      ArrayList myArrayList = new ArrayList();

      // use the add method
      myArrayList.Add("Hello");
      myArrayList.Add("TutorialsPoint");
      myArrayList.Add("India");
      myArrayList.Add("Hyderabad");

      // Displays the ArrayList.
      Console.WriteLine("The ArrayList contains the following:");
      PrintValues(myArrayList, ' ');
   }

   public static void PrintValues(IEnumerable myList, char mySeparator) {
      foreach(object obj in myList) {
         Console.Write("{0}{1}", mySeparator, obj);
      }
      Console.WriteLine();
   }
}

Output

Following is the output −

The ArrayList contains the following:
 Hello TutorialsPoint India Hyderabad

Example 3: Get The Index Value

In this example, we retrieve the index of the added element in the ArrayList by using the Add() method −

using System;
using System.Collections;

public class SamplesArrayList {
   public static void Main() {
      // Creates and initializes a new ArrayList.
      ArrayList myArrayList = new ArrayList();
   
      // use the add method
    
      int indx1 = myArrayList.Add("TutorialsPoint");
      int indx2 = myArrayList.Add("India");
   
      // Displays the index.
      Console.WriteLine("Index Value of the Added element is {0}, and {1}:", indx1, indx2);
   
   }
}

Output

Following is the output −

Index Value of the Added element is 0, and 1:

Example 4: Adding Mixed Data Types to an ArrayList

In this example, we use the Add() method to add the objects of different data types in the ArrayList −

using System;
using System.Collections;
public class Example
{
   public static void Main()
   {
      // Create an ArrayList
      ArrayList myArrayList = new ArrayList();

      // Add different types of objects to the ArrayList
      myArrayList.Add("Hello");
      myArrayList.Add(80);
      myArrayList.Add(3.14);
      myArrayList.Add(DateTime.Now);
      myArrayList.Add(null);

      // Display the elements in the ArrayList
      Console.WriteLine("ArrayList contents:");
      foreach (object item in myArrayList)
      {
         Console.WriteLine(item == null ? "null" : item.ToString());
      }

      // Add a new object and get its index
      int index = myArrayList.Add("New Item");

      Console.WriteLine($"\nThe new item was added at index: {index}");
   }
}

Output

Following is the output −

ArrayList contents:
Hello
80
3.14
1/22/2025 11:29:31 AM
null

The new item was added at index: 5
csharp_arraylist.htm
Advertisements