C# | Insert an element into the ArrayList at the specified index Last Updated : 01 Feb, 2019 Comments Improve Suggest changes Like Article Like Report 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.Insert(Int32, Object) method inserts an element into the ArrayList at the specified index. Properties of ArrayList Class: Elements can be added or removed from the Array List collection at any point in time. The ArrayList is not guaranteed to be sorted. The capacity of an ArrayList is the number of elements the ArrayList can hold. Elements in this collection can be accessed using an integer index. Indexes in this collection are zero-based. It also allows duplicate elements. Using multidimensional arrays as elements in an ArrayList collection is not supported. Syntax : public virtual void Insert (int index, object value); Parameters: index : It is the zero-based index at which value should be inserted. value : It is the Object which is to be inserted. The value can be null. Exceptions: ArgumentOutOfRangeException : If index is less than zero or index is greater than Count i.e, number of elements in the ArrayList. NotSupportedException : If the ArrayList is read-only or the ArrayList has a fixed size. Below given are some examples to understand the implementation in a better way: Example 1: CSHARP // C# code to insert an element // into the ArrayList at the // specified index using System; using System.Collections; using System.Collections.Generic; class GFG { // Driver code public static void Main() { // Creating an ArrayList ArrayList myList = new ArrayList(10); // Adding elements to ArrayList myList.Add("A"); myList.Add("B"); myList.Add("C"); // Displaying the elements in ArrayList Console.WriteLine("The initial ArrayList is : "); foreach(string str in myList) { Console.WriteLine(str); } // Inserting 3 elements at random index in the ArrayList myList.Insert(1, "D"); myList.Insert(4, "E"); myList.Insert(5, "F"); // Displaying the modified ArrayList Console.WriteLine("The ArrayList after Inserting elements is : "); // Displaying the elements in ArrayList foreach(string str in myList) { Console.WriteLine(str); } } } Output: The initial ArrayList is : A B C The ArrayList after Inserting elements is : A D B C E F Example 2: CSHARP // C# code to insert an element // into the ArrayList at the // specified index using System; using System.Collections; using System.Collections.Generic; class GFG { // Driver code public static void Main() { // Creating an ArrayList ArrayList myList = new ArrayList(10); // Adding elements to ArrayList myList.Add(1); myList.Add(2); myList.Add(3); // Displaying the elements in ArrayList Console.WriteLine("The initial ArrayList is : "); foreach(int i in myList) { Console.WriteLine(i); } // Inserting 3 elements in front of the ArrayList myList.Insert(0, 4); myList.Insert(0, 5); myList.Insert(0, 6); // Displaying the modified ArrayList Console.WriteLine("The ArrayList after Inserting elements is : "); // Displaying the elements in ArrayList foreach(int i in myList) { Console.WriteLine(i); } } } Output: The initial ArrayList is : 1 2 3 The ArrayList after Inserting elements is : 6 5 4 1 2 3 Note: If index is equal to Count i.e, number of elements in the ArrayList, value is added to the end of ArrayList. This method is an O(n) operation, where n is Count. Reference: https://docs.microsoft.com/en-us/dotnet/api/system.collections.arraylist.insert?view=netframework-4.7.2 Comment More infoAdvertise with us Next Article C# | Insert an element into the ArrayList at the specified index S Sahil_Bansall Follow Improve Article Tags : Misc C# CSharp-method CSharp-Collections-Namespace CSharp-Collections-ArrayList +1 More Practice Tags : Misc Similar Reads C# | Remove the element at the specified index 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.RemoveAt(Int32) method is used to remove the element at the speci 3 min read C# | Insert an element into Collection<T> at specified index Collection<T>.Insert(Int32, T) method is used to insert an element into the Collection<T> at the specified index. Syntax: public void Insert (int index, T item); Parameters: index : The zero-based index at which item should be inserted. item : The object to insert. The value can be null 3 min read C# | How to insert the elements of a collection into the List at the specified index List<T>.InsertRange(Int32, IEnumerable<T>) Method is used to insert the elements of a collection into the List<T> at the specified index. Properties of List: It is different from the arrays. A list can be resized dynamically but arrays cannot. List class can accept null as a valid 3 min read C# | Gets or Sets the element at the specified index in the List List<T>.Item[Int32] Property is used to gets or sets the element at the specified index. Properties of List: It is different from the arrays. A list can be resized dynamically but arrays cannot. List class can accept null as a valid value for reference types and it also allows duplicate elemen 2 min read 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# | 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 cann 2 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 C# | Creating an ArrayList having specified initial capacity ArrayList(Int32) constructor is used to initialize a new instance of the ArrayList class which will be empty and will have the specified initial capacity. ArrayList represents an ordered collection of an object that can be indexed individually. It is basically an alternative to an array. It also all 3 min read C# | Insert at the specified index in StringCollection StringCollection class is a new addition to the .NET Framework class library that represents a collection of strings. StringCollection class is defined in the System.Collections.Specialized namespace. StringCollection.Insert(Int32, String) method is used to insert a string into the StringCollection 2 min read C# | Get the number of elements actually contained in 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.Count property gets the number of elements actually contained in 3 min read Like