How to pop the first element of a list in C#

1 Answer

0 votes
using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        List<int> list = new List<int> { 1, 2, 3, 4, 5 };

        // Check if the list is not empty
        if (list.Count > 0) {
            // Remove the first element
            list.RemoveAt(0);
        }

        // Print the updated list
        foreach (int num in list) {
            Console.Write(num + " ");
        }
    }
}



/*
run:

2 3 4 5

*/

 



answered May 2, 2025 by avibootz
...