public Insert ( int index, Object value ) : void | ||
index | int | |
value | Object | |
return | void |
using System.Collections; // create an ArrayList with some initial values ArrayList list = new ArrayList(); list.Add("apple"); list.Add("banana"); list.Add("cherry"); // insert an element at index 1 list.Insert(1, "orange"); // print the updated list foreach (var item in list) { Console.WriteLine(item); }
apple orange banana cherryIn this example, we create an ArrayList and add three fruits to it. We then use the Insert() method to add "orange" at index 1, shifting the other elements down one index. Finally, we iterate over the updated ArrayList and print each element to the console. The ArrayList class and the Insert() method are part of the System.Collections package library in C#.