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 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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
        }
    }
}

chevron_right


Output:

Elements in List: 

Geeks
for
Geeks
 
InsertRange(2, str1)

Geeks
for
New
Element
Added
Geeks

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
        }
    }
}

chevron_right


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:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.