C# ArrayList - AddRange() Method



The C# ArrayList AddRange() method is used to add the elements of an Icollection to the end of the ArrayList.

Icollection represents a collection object that provides basic operations like adding, removing, checking the count of elements, and accessing the collection as an array. The collection itself can't be null, but it can contain the null elements.

Syntax

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

public virtual void AddRange (System.Collections.ICollection c);

Parameters

This method takes an ICollection parameter to add its elements to the end of the ArrayList−

Return value

This method does not return a value.

Example 1: Add Elements in Range

This is the basic example of the ArrayList collection to demonstrate the use of the AddRange() method −

    
using System;
using System.Collections;
class Program
{
   static void Main()
   {
      ArrayList arrayList = new ArrayList();
      // Create another collection
      string[] itemsToAdd = { "Item1", "Item2", "Item3" };
      arrayList.AddRange(itemsToAdd);

      // Display the elements of the ArrayList
      foreach (var item in arrayList)
      {
         Console.WriteLine(item);
      }
   }
}

Output

Following is the output −

Item1
Item2
Item3

Example 2: Add an Integer Array

Let us see another example of the AddRange() method of the ArrayList to add the integer array in the ArrayList −

using System;
using System.Collections;
class Program
{
   static void Main()
   {
      ArrayList arrayList = new ArrayList();

      // Create another collection
      int[] itemsToAdd = { 1, 2, 3, 4, 5 };
      arrayList.AddRange(itemsToAdd);

      // Display the elements of the ArrayList
      Console.WriteLine("Items of ArrayList -");
      foreach (var item in arrayList)
      {
         Console.Write(item + " ");
      }
   }
}

Output

Following is the output −

Items of ArrayList -
1 2 3 4 5

Example 3: Adding Element of Another ArrayList

In this example, we use the AddRange method to add the element in ArrayList of an ArrayList −

using System;
using System.Collections;
class Program
{
   static void Main()
   {
      // Create the first ArrayList
      ArrayList arrayList1 = new ArrayList(){10, 20};

      // Create another ArrayList
      ArrayList arrayList2 = new ArrayList();
      arrayList2.Add(30);
      arrayList2.Add(40);

      // Add all elements from arrayList2 to arrayList1
      arrayList1.AddRange(arrayList2);
      
      Console.WriteLine("Elements of the ArrayList1 - ");
      foreach (var item in arrayList1)
      {
          Console.Write(item + " ");
      }
   }
}

Output

Following is the output −

Elements of the ArrayList1 - 
10 20 30 40

Example 4: Adding Elements from a List

In this example, we use the AddRange() method to add the element of a list into the ArrayList −

using System;
using System.Collections;
using System.Collections.Generic;
class Program
{
   static void Main()
   {
      ArrayList arrayList = new ArrayList(){"A", "b"};

      // Create a List of strings (ICollection)
      List<string> list = new List<string> { "C", "D", "E" };

      // Add all elements from the List to the ArrayList
      arrayList.AddRange(list);
      Console.WriteLine("Elements of the ArrayList");
      foreach (var item in arrayList)
      {
         Console.Write(item + " ");
      }
   }
}

Output

Following is the output −

Elements of the ArrayList
A b C D E 
csharp_arraylist.htm
Advertisements