How to add an item to an ArrayList in C#?


ArrayList is a non-generic type of collection in C# that dynamically resizes.

Let us see how to initialize ArrayList in C# −

ArrayList arr= new ArrayList();

Add an item to an Array List −

ArrayList arr1 = new ArrayList();
arr1.Add(30);
arr1.Add(70);

Let us see the complete example to implement ArrayList in C#. Here we have two array lists. The 2nd array list is appended to the first list.

Example

using System;
using System.Collections;

public class MyClass {
   public static void Main() {
      ArrayList arr1 = new ArrayList();
      arr1.Add(30);
      arr1.Add(70);

      ArrayList arr2 = new ArrayList();
      arr2.Add(200);
      arr2.Add(240);

      arr1.AddRange(arr2);
      for (int i = 0; i < arr1.Count; i++)
      Console.WriteLine(arr1[i]);
   }
}

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 21-Jun-2020

113 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements