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 value for reference types and it also allows duplicate elements.
- If the Count becomes equals to Capacity then the capacity of the List increases automatically by reallocating the internal array. The existing elements will be copied to the new array before the addition of the new element.
public void InsertRange (int index, System.Collections.Generic.IEnumerable<T> collection);
index: It is the zero-based index at which the new elements should be inserted.
collection: It is the collection whose elements will be inserted into the List<T>
Note: The collection itself cannot be null. But it can contain elements which can be null if the type T is a reference type.
- ArgumentNullException: If the collection is null.
- ArgumentOutOfRangeException: If the index is less than zero or greater than count.
Below programs illustrate the use of above discussed method:
Elements in List: Geeks for Geeks InsertRange(2, str1) Geeks for New Element Added Geeks
System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
- C# | Insert an element into Collection<T> at specified index
- C# | Adding the elements of the specified collection to the end of the List
- 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# | Get or set the element at specified index in Collection<T>
- C# | Searching the index of specified object in Collection<T>
- C# | Remove element at specified index of Collection<T>
- C# | Insert at the specified index in StringCollection
- C# | Insert into OrderedDictionary with key and value at specified index
- C# | Insert an element into the ArrayList at the specified index
- C# | Copy the elements of collection over a range of elements in ArrayList
- C# | Remove all elements from the Collection<T>
- C# | Remove all elements in a collection from a HashSet
- C# | Copying the Collection<T> elements to an array
- C# | Check if HashSet and the specified collection contain the same elements
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.
Improved By : nidhi_biet