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:
- 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 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:
Recommended Posts:
- C# | Remove element at specified index of Collection<T>
- C# | Remove the element at the specified index of the ArrayList
- C# | Gets or Sets the element at the specified index in the List
- C# | Remove from the specified index of the StringCollection
- C# | Remove from the specified index of a SortedList
- C# | Remove the entry at specified index from OrderedDictionary
- 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# | Insert an element into the ArrayList at the specified index
- C# | Gets or sets the element at the specified index in StringCollection
- C# | Finding the index of last element in the array
- C# | Insert an element into Collection<T> at specified index
- C# | Finding the index of first element in the array
- C# | Remove the specified element from a HashSet
- C# | Remove the element with the specified key from the Hashtable
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.



