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

CsharpProgrammingServer Side Programming

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


raja
Published on 20-Aug-2018 12:25:37
Advertisements