C# | Adding the elements to the end of the ArrayList

ArrayList.AddRange(ICollection) Method is used to add the elements of an ICollection to the end of the ArrayList.

Syntax:

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

Here, c is the ICollection whose elements should be added to the end of the ArrayList. The collection itself cannot be null, but it can contain elements that are null.

Exceptions:

  • ArgumentException: If c is null
  • NotSupportedException: If the ArrayList is read-only or ArrayList has a fixed size.

Below programs illustrate the use of the above-discussed method:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# code to add the elements of an
// ICollection to the end of the ArrayList
using System;
using System.Collections;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating an ArrayList
        ArrayList myList = new ArrayList();
  
        // Adding elements to ArrayList
        myList.Add("A");
        myList.Add("B");
        myList.Add("C");
        myList.Add("D");
        myList.Add("E");
        myList.Add("F");
  
        Console.WriteLine("Before AddRange Method");
        Console.WriteLine();
  
        // displaying the item of myList
        foreach(String str in myList)
        {
            Console.WriteLine(str);
        }
  
        Console.WriteLine("\nAfter AddRange Method\n");
  
        // Here we are using AddRange method
        // Which adds the elements of myList
        // Collection in myList again i.e.
        // we have copied the whole myList
        // in it
        myList.AddRange(myList);
  
        // displaying the item of List
        foreach(String str in myList)
        {
            Console.WriteLine(str);
        }
    }
}

chevron_right


Output:

Before AddRange Method

A
B
C
D
E
F

After AddRange Method

A
B
C
D
E
F
A
B
C
D
E
F

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# code to add the elements of an
// ICollection to the end of the ArrayList
using System;
using System.Collections;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating an ArrayList
        ArrayList myList = new ArrayList();
  
        // adding elements in myList
        myList.Add("Geeks");
        myList.Add("GFG");
        myList.Add("C#");
        myList.Add("Tutorials");
  
        Console.WriteLine("Before AddRange Method");
        Console.WriteLine();
  
        // displaying the item of myList
        foreach(String str in myList)
        {
            Console.WriteLine(str);
        }
  
        Console.WriteLine("\nAfter AddRange Method\n");
  
        // taking array of String
        string[] str_add = { "Collections",
                             "Generic",
                             "List" };
  
        // here we are adding the elements
        // of the str_add to the end of
        // the myList
        myList.AddRange(str_add);
  
        // displaying the item of List
        foreach(String str in myList)
        {
            Console.WriteLine(str);
        }
    }
}

chevron_right


Output:

Before AddRange Method

Geeks
GFG
C#
Tutorials

After AddRange Method

Geeks
GFG
C#
Tutorials
Collections
Generic
List

Note:

  • ArrayList accepts null as a valid value and allows duplicate elements.
  • The order of the elements in the ICollection is preserved in the ArrayList.

Reference:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.