How to remove an item from a C# list by using an index?


To remove an item from a list in C# using index, use the RemoveAt() method.

Firstly, set the list −

List<string> list1 = new List<string>() {
   "Hanks",
   "Lawrence",
   "Beckham",
   "Cooper",
};

Now remove the element at 2nd position i.e. index 1

list1.RemoveAt(1);

Let us see the complete example −

Example

 Live Demo

using System;
using System.Collections.Generic;
using System.Linq;

class Program {
   static void Main() {
      List<string> list1 = new List<string>() {
         "Hanks",
         "Lawrence",
         "Beckham",
         "Cooper",
      };

      Console.Write("Initial list...");
      foreach (string list in list1) {
         Console.WriteLine(list);
      }

      Console.Write("Removing element from the list...");
      list1.RemoveAt(1);
   
      foreach (string list in list1) {
         Console.WriteLine(list);
      }
   }
}

Output

Initial list...
Hanks
Lawrence
Beckham
Cooper
Removing element from the list...
Hanks
Beckham
Cooper

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 22-Jun-2020

12K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements