Open In App

C# | How to remove the element from the specified index of the List

List<T>.RemoveAt (Int32) Method is used to remove the element at the specified index of the List<T>.

Properties of List:



Syntax:

public void RemoveAt (int index);

Parameters:



index: It is the zero-based starting index of the element which is to be removed.
count: It is the number of the elements which is to be removed.

Exceptions: This method will give ArgumentOutOfRangeException if the index is less than 0 or equal to or greater than Count.

Below programs illustrate the use of List<T>.RemoveAt (Int32) Method:

Example 1:




// C# Program to remove the element at
// the specified index of the List<T>
using System;
using System.Collections;
using System.Collections.Generic;
  
class Geeks {
  
    // Main Method
    public static void Main(String[] args)
    {
  
        // Creating an List<T> of Integers
        List<int> firstlist = new List<int>();
  
        // Adding elements to List
        firstlist.Add(17);
        firstlist.Add(19);
        firstlist.Add(21);
        firstlist.Add(9);
        firstlist.Add(75);
        firstlist.Add(19);
        firstlist.Add(73);
  
        Console.WriteLine("Elements Present in List:\n");
  
        int p = 0;
  
        // Displaying the elements of List
        foreach(int k in firstlist)
        {
            Console.Write("At Position {0}: ", p);
            Console.WriteLine(k);
            p++;
        }
  
        Console.WriteLine(" ");
  
        // removing the element at index 3
        Console.WriteLine("Removing the element at index 3\n");
  
        // 9 will remove from the List
        // and 75 will come at index 3
        firstlist.RemoveAt(3);
  
        int p1 = 0;
  
        // Displaying the elements of List
        foreach(int n in firstlist)
        {
            Console.Write("At Position {0}: ", p1);
            Console.WriteLine(n);
            p1++;
        }
    }
}

Output:

Elements Present in List:

At Position 0: 17
At Position 1: 19
At Position 2: 21
At Position 3: 9
At Position 4: 75
At Position 5: 19
At Position 6: 73
 
Removing the element at index 3

At Position 0: 17
At Position 1: 19
At Position 2: 21
At Position 3: 75
At Position 4: 19
At Position 5: 73

Example 2:




// C# Program to remove the element at
// the specified index of the List<T>
using System;
using System.Collections;
using System.Collections.Generic;
  
class Geeks {
  
    // Main Method
    public static void Main(String[] args)
    {
  
        // Creating an List<T> of Integers
        List<int> firstlist = new List<int>();
  
        // Adding elements to List
        firstlist.Add(17);
        firstlist.Add(19);
        firstlist.Add(21);
        firstlist.Add(9);
        firstlist.Add(75);
        firstlist.Add(19);
        firstlist.Add(73);
  
        Console.WriteLine("Elements Present in List:\n");
  
        int p = 0;
  
        // Displaying the elements of List
        foreach(int k in firstlist)
        {
            Console.Write("At Position {0}: ", p);
            Console.WriteLine(k);
            p++;
        }
  
        Console.WriteLine(" ");
  
        // removing the element at index 3
        Console.WriteLine("Removing the element at index 3\n");
  
        // taking negative index
        // it will give error as index
        // cannot be less than 0
        firstlist.RemoveAt(-1);
  
        int p1 = 0;
  
        // Displaying the elements of List
        foreach(int n in firstlist)
        {
            Console.Write("At Position {0}: ", p1);
            Console.WriteLine(n);
            p1++;
        }
    }
}

Runtime 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

Output:

Elements Present in List:

At Position 0: 17
At Position 1: 19
At Position 2: 21
At Position 3: 9
At Position 4: 75
At Position 5: 19
At Position 6: 73
 
Removing the element at index 3

Reference:


Article Tags :
C#