Collection<T>.Insert(Int32, T) method is used to insert an element into the Collection<T> at the specified index.
public void Insert (int index, T item);
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:
Count : 5 A B C D E Count : 6 A B GFG C D E
System.ArgumentOutOfRangeException: Index must be within the bounds of the List.
Parameter name: index
- 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.
- C# | Insert an element into the ArrayList at the specified index
- C# | Insert at the specified index in StringCollection
- C# | Insert into OrderedDictionary with key and value at specified index
- C# | How to insert the elements of a collection into the List at the specified index
- C# | How to insert an element in an Array?
- C# | Get or set the element at the specified index in ArrayList
- C# | Get or set the element at specified index in Collection<T>
- C# | Remove element at specified index of Collection<T>
- C# | How to remove the element from the specified index of the List
- C# | Finding the index of first element in the array
- C# | Gets or sets the element at the specified index in StringCollection
- Finding the Index which Points Beyond the Last Element in C#
- C# | Finding the index of last element in the array
- C# | Remove the element at the specified index of the ArrayList
- Finding the Index of First Element of the Specified Sequence in C#
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.