.Net framework ArrayList AddRange() method allow us to add the elements of an ICollection to the end of the ArrayList. this arraylist AddRange() method exists under System.Collections namespace.
this method require to pass a parameter named ‘c’. the ‘c’ parameter type is System.Collections.ICollection, which represents 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.
the AddRange() method throw ArgumentNullException exception, if the ‘c’ is null. this method also throw NotSupportedException, if the ArrayList is read-only or the ArrayList has a fixed size. arraylist accepts duplicate values.
the following console c# example code demonstrate us how can we add a collection of elements to the end of an arraylist programmatically at run time in an console application.
C# Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | class Program { static void Main(string[] args) { ArrayList colors = new ArrayList() { "Green", "SeaGreen", "SpringGreen" }; List<string> redColors = new List<string> { "Red", "IndianRed", "DarkRed" }; colors.AddRange(redColors); Console.WriteLine("ArrayList Elements...."); foreach (string color in colors) { Console.WriteLine(color); } Console.ReadLine(); } } |
Output:
1 2 3 4 5 6 7 8 9 | ArrayList Elements.... Green SeaGreen SpringGreen Red IndianRed DarkRed |