public InsertRange ( int index, ICollection c ) : void | ||
index | int | |
c | ICollection | |
return | void |
using System.Collections; ArrayList list1 = new ArrayList(); list1.Add("apple"); list1.Add("banana"); list1.Add("orange"); list1.Add("grapefruit"); ArrayList list2 = new ArrayList(); list2.Add("strawberry"); list2.Add("blueberry"); list1.InsertRange(3, list2); // insert list2 into list1 starting at index position 3 // Output the final list1: foreach (string item in list1) { Console.WriteLine(item); }
using System.Collections; ArrayList list1 = new ArrayList(); list1.Add("apple"); list1.Add("banana"); list1.Add("orange"); ArrayList list2 = new ArrayList(); list2.Add("strawberry"); list2.Add("blueberry"); try { list1.InsertRange(4, list2); // try to insert list2 into list1 at index position 4 (invalid) } catch (ArgumentOutOfRangeException ex) { Console.WriteLine(ex.Message); // output the error message } // Output the final list1: foreach (string item in list1) { Console.WriteLine(item); }Output: - Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index - apple - banana - orange Package library: System.Collections.