C# ArrayList - Insert() Method



The C# ArrayList Insert() method is used to add or insert item into the arrayList at the specified index position.

Syntax

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

public virtual void Insert (int index, object? value);

Parameters

This method accepts the following parameters −

  • index: It is an index value where we want to insert the element.
  • value: It is an element that will inserted in the ArrayList. It can be null.

Return value

This method does not return any value.

Example 1: Insert an Integer at Given Index

Following is the basic example of the Insert() method to insert an element in the ArrayList at the specified index −

using System;
using System.Collections;
class Program
{
   static void Main()
   {
      ArrayList arrayList = new ArrayList { 1, 2, 3, 4 };
      
      Console.Write("Before Insert: ");
      foreach (var item in arrayList)
      {
         Console.Write(item + " ");
      }
      
      // insert 5 at the index 4
      arrayList.Insert(4, 5);
      
      Console.Write( "\nAfter Insert: ");
      foreach (var item in arrayList)
      {
         Console.Write(item + " ");
      }
   }
}

Output

Following is the output −

Before Insert: 1 2 3 4 
After Insert: 1 2 3 4 5

Example 2: Insert a Character at Given Index

Let's see another example where we insert a character into the ArrayList at a specified index position using the Insert() method −

using System;
using System.Collections;
class Program
{
   static void Main()
   {
      ArrayList arrayList = new ArrayList { "A", "B", "C", "D" };
      
      Console.Write("Before Insert: ");
      foreach (var item in arrayList)
      {
         Console.Write(item + " ");
      }
      
      // insert E at the index 2
      arrayList.Insert(2, "E");
      
      Console.Write( "\nAfter Insert: ");
      foreach (var item in arrayList)
      {
         Console.Write(item + " ");
      }
   }
}

Output

Following is the output −

Before Insert: A B C D 
After Insert: A B E C D

Example 3: Insert Both Integer and Character in an ArrayList

The below example creates an ArrayList with some elements. We then use the Insert() method to insert the element at the specified position into the created ArrayList. −

using System;
using System.Collections;
class Program
{
   static void Main()
   {
      ArrayList arrayList = new ArrayList { "A", "B", "C", "D" };
      
      Console.Write("Before Insert: ");
      foreach (var item in arrayList)
      {
         Console.Write(item + " ");
      }
      
      // insert a integer at index 2
      arrayList.Insert(2, 2);
      // insert a character at index 3
      arrayList.Insert(3, "A");
      
      Console.Write( "\nAfter Insert: ");
      foreach (var item in arrayList)
      {
         Console.Write(item + " ");
      }
   }
}

Output

Following is the output −

Before Insert: A B C D 
After Insert: A B 2 A C D 

Example 4: Insert at Beginning

Here, we creates an ArrayList with some elements. We then use the Insert() method to insert the element at the beginning of the ArrayList −

using System;
using System.Collections;
class Program
{
   static void Main()
   {
      ArrayList arrayList = new ArrayList { "tutorialspoint", "India", "Hyderabad", 500081 };
      
      Console.Write("Before Insert: ");
      foreach (var item in arrayList)
      {
         Console.Write(item + " ");
      }
      
      // insert an element at Begenning
      // of the ArrayList
      arrayList.Insert(0, "Aman");

      
      Console.Write( "\nAfter Insert: ");
      foreach (var item in arrayList)
      {
         Console.Write(item + " ");
      }
   }
}

Output

Following is the output −

Before Insert: tutorialspoint India Hyderabad 500081 
After Insert: Aman tutorialspoint India Hyderabad 500081 
csharp_arraylist.htm
Advertisements