C# | How to insert the elements of a collection into the List at the specified index Last Updated : 18 Oct, 2019 Comments Improve Suggest changes Like Article Like Report 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. Syntax: public void InsertRange (int index, System.Collections.Generic.IEnumerable<T> collection); Parameter: 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. Exceptions: 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: Example 1: CSharp // C# Program to insert the elements of // a collection into the List<T> at the // specified index using System; using System.Collections; using System.Collections.Generic; class Geeks { // Main Method public static void Main(String[] args) { string[] str1 = { "Geeks", "for", "Geeks" }; // Creating an List<T> of strings // adding str1 elements to List List<String> firstlist = new List<String>(str1); // displaying the elements of firstlist Console.WriteLine("Elements in List: \n"); foreach(string dis in firstlist) { Console.WriteLine(dis); } Console.WriteLine(" "); // contains new Elements which is // to be added in the List str1 = new string[] { "New", "Element", "Added" }; // using InsertRange Method Console.WriteLine("InsertRange(2, str1)\n"); // adding elements after 2nd // index of the List firstlist.InsertRange(2, str1); // displaying the elements of // List after InsertRange Method foreach(string res in firstlist) { Console.WriteLine(res); } } } Output: Elements in List: Geeks for Geeks InsertRange(2, str1) Geeks for New Element Added Geeks Example 2: CSharp // C# Program to insert the elements of // a collection into the List<T> at the // specified index using System; using System.Collections; using System.Collections.Generic; class Geeks { // Main Method public static void Main(String[] args) { string[] str1 = { "Geeks", "for", "Geeks" }; // Creating an List<T> of strings // adding str1 elements to List List<String> firstlist = new List<String>(str1); // displaying the elements of firstlist Console.WriteLine("Elements in List: \n"); foreach(string dis in firstlist) { Console.WriteLine(dis); } Console.WriteLine(" "); // contains new Elements which is // to be added in the List str1 = new string[] { "New", "Element", "Added" }; // using InsertRange Method Console.WriteLine("InsertRange(2, str1)\n"); // this will give error as // index is less than 0 firstlist.InsertRange(-1, str1); // displaying the elements of // List after InsertRange Method foreach(string res in firstlist) { Console.WriteLine(res); } } } Error: Unhandled Exception: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index Reference: https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.insertrange?view=netframework-4.7.2 Comment More infoAdvertise with us Next Article C# | How to insert the elements of a collection into the List at the specified index K Kirti_Mangal Follow Improve Article Tags : Python C# CSharp-method CSharp-Generic-List CSharp-Collections-Namespace CSharp-Generic-Namespace +2 More Practice Tags : python Similar Reads 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# | Adding the elements of the specified collection to the end of the List List<T>.AddRange(IEnumerable<T>) Method is used to add the elements of the specified collection to the end of the List<T>. 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 refer 3 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 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 Like