C# | Adding the elements to the end of the ArrayList Last Updated : 01 Feb, 2019 Comments Improve Suggest changes Like Article Like Report 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: CSharp // 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); } } } 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: CSharp // 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); } } } 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: https://docs.microsoft.com/en-us/dotnet/api/system.collections.arraylist.addrange?view=netframework-4.7.2 Comment More infoAdvertise with us Next Article C# | Adding the elements to the end of the ArrayList K Kirti_Mangal Follow Improve Article Tags : C# CSharp-method CSharp-Collections-Namespace CSharp-Collections-ArrayList Similar Reads C# | Adding elements to the end of the ArrayList AddRange(ICollection) Method is used to add the elements of an ICollection to the end of the ArrayList. Or in other words, this method is used to add the multiple elements from other collection into an ArrayList. Here elements are defined as the primitive or non-primitive type. Syntax: public virtua 3 min read C# | Add an object to the end of the ArrayList ArrayList represents an ordered collection of an object that can be indexed individually. It is basically an alternative to an array. It also allows dynamic memory allocation, adding, searching and sorting items in the list. ArrayList.Add(Object) method adds an object to the end of the ArrayList. Pr 2 min read C# | Remove a range of elements from the ArrayList ArrayList represents an ordered collection of an object that can be indexed individually. It is basically an alternative to an array. It also allows dynamic memory allocation, adding, searching and sorting items in the list. ArrayList.RemoveRange(Int32, Int32) method is used to remove a range of ele 3 min read C# | Sets the capacity to the actual number of elements in the ArrayList ArrayList.TrimToSize Method is used to set the capacity to the actual number of elements in the ArrayList. It can be used to minimize a collection's memory overhead if no new elements will be added to the collection. Note: This method is an O(n) operation, where n is Count. Syntax: public virtual vo 3 min read C# | Getting a subset of the elements from the source ArrayList ArrayList.GetRange(Int32, Int32) Method is used to get an ArrayList which will represent a subset of the elements in the source ArrayList. Syntax: public virtual System.Collections.ArrayList GetRange (int index, int count); Parameters: index: It is of Int32 type and represents the zero-based ArrayLi 3 min read Like