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:
// 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:
// 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:
Recommended Posts:
- C# | Insert an element into the ArrayList at the specified index
- C# | How to insert the elements of a collection into the List at the specified index
- C# | Insert into OrderedDictionary with key and value at specified index
- C# | Insert at the specified index in StringCollection
- Creating an Index From the Specified Index at the Start of a Collection in C#
- Creating an Index From the End of a Collection at a Specified Index Position in C#
- C# | Insert a new entry in OrderedDictionary with specified key and value
- C# | Gets or Sets the element at the specified index in the List
- C# | Remove the element at the specified index of the ArrayList
- C# | How to remove the element from the specified index of the List
- C# | Remove element at specified index of Collection<T>
- C# | Get or set the element at specified index in Collection<T>
- C# | Get or set the element at the specified index in ArrayList
- C# | Gets or sets the element at the specified index in StringCollection
- Finding the Index of First Element of the Specified Sequence in C#
- C# | Get or Set at specified index in StringCollection
- C# | Remove from the specified index of the StringCollection
- C# | Copy StringCollection at the specified index of array
- C# | Remove from the specified index of a SortedList
- C# | Copy StringDictionary to Array at the specified index
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

