How to pop the first element from a C# List?


To pop the first element in the list, use the RemoveAt() method. It eliminates the element from the position you want to remove the element.

Set the list

List<string> myList = new List<string>() {
   "Operating System",
   "Computer Networks",
   "Compiler Design"
};

Now pop the first element using RemoveAt(0)

myList.RemoveAt(0);

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> myList = new List<string>() {
         "Operating System",
         "Computer Networks",
         "Compiler Design"
      };

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

      Console.Write("Removing first element from the list...");
      myList.RemoveAt(0);

      foreach (string list in myList) {
         Console.WriteLine(list);
      }
   }
}

Output

Initial list...
Operating System
Computer Networks
Compiler Design
Removing first element from the list...
Computer Networks
Compiler Design

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 31-Oct-2023

22K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements