C# | Insert an element into Collection<T> at specified index Last Updated : 01 Feb, 2019 Comments Improve Suggest changes Like Article Like Report 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 for reference types. Exception: This method will give ArgumentOutOfRangeException if index is less than zero OR index is greater than Count. Below given are some examples to understand the implementation in a better way: Example 1: CSHARP // C# code to insert an element into // the Collection at the specified index using System; using System.Collections.Generic; using System.Collections.ObjectModel; class GFG { // Driver code public static void Main() { // Creating a collection of strings Collection<string> myColl = new Collection<string>(); // Adding elements in Collection myColl myColl.Add("A"); myColl.Add("B"); myColl.Add("C"); myColl.Add("D"); myColl.Add("E"); // Displaying the number of elements in myColl Console.WriteLine("Count : " + myColl.Count); // Displaying the elements in myColl foreach(string str in myColl) { Console.WriteLine(str); } // Inserting an element into the // Collection at the specified index myColl.Insert(2, "GFG"); // Displaying the number of elements in myColl Console.WriteLine("Count : " + myColl.Count); // Displaying the elements in myColl foreach(string str in myColl) { Console.WriteLine(str); } } } Output: Count : 5 A B C D E Count : 6 A B GFG C D E Example 2: CSHARP // C# code to insert an element into // the Collection at the specified index using System; using System.Collections.Generic; using System.Collections.ObjectModel; class GFG { // Driver code public static void Main() { // Creating a collection of ints Collection<int> myColl = new Collection<int>(); // Adding elements in Collection myColl myColl.Add(2); myColl.Add(3); myColl.Add(4); myColl.Add(5); // Displaying the number of elements in myColl Console.WriteLine("Count : " + myColl.Count); // Displaying the elements in myColl foreach(int i in myColl) { Console.WriteLine(i); } // Inserting an element into the // Collection at the specified index // This should raise "ArgumentOutOfRangeException" // as index is less than 0 myColl.Insert(-1, 8); // Displaying the number of elements in myColl Console.WriteLine("Count : " + myColl.Count); // Displaying the elements in myColl foreach(int i in myColl) { Console.WriteLine(i); } } } Runtime Error: Unhandled Exception: System.ArgumentOutOfRangeException: Index must be within the bounds of the List. Parameter name: index Note: Collection<T> accepts null as a valid value for reference types and allows duplicate elements. If index is equal to Count, item is added to the end of Collection<T>. This method is an O(n) operation, where n is Count. Reference: https://docs.microsoft.com/en-us/dotnet/api/system.collections.objectmodel.collection-1.insert?view=netframework-4.7.2 Comment More infoAdvertise with us Next Article C# | Insert an element into Collection<T> at specified index S Sahil_Bansall Follow Improve Article Tags : C# CSharp-method CSharp-Collections.ObjectModel-Namespace CSharp-Collection-Class Similar Reads 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# | Remove element at specified index of Collection<T> Collection<T>.RemoveAt(Int32) is used to remove the element at the specified index of the Collection<T>. Syntax: public void RemoveAt (int index); Here, index is the zero-based index of the element to remove. Exception: This method will give ArgumentOutOfRangeException if the index is le 2 min read C# | Get or set the element at specified index in Collection<T> Collection<T>.Item[Int32] property is used to get or set the element at the specified index. Syntax: public T this[int index] { get; set; } Here, index is the zero-based index of the element to get or set. Return Value: The element at the specified index. Exception: This method will give Argum 3 min read C# | Insert an element into the ArrayList at the specified index 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 ArrayLis 3 min read Creating an Index From the End of a Collection at a Specified Index Position in C# The Index Structure is introduced in C# 8.0. It represents a type that can be used to index a collection or sequence and it can be started from the start or the end. You are allowed to create an end index with the help of the FromEnd(Int32) Method provided by the Index struct. This method returns an 2 min read Like