How to add an item to the beginning of a list in C#

1 Answer

0 votes
using System;
using System.Collections.Generic;
 
class AddItemToBeginningOfAList_CSharp
{
    static void Main(string[] args)
    {
        List<string> list = new List<string>()
        {
            "c#",
            "java",
            "c",
            "c++"
        };
            
        list.Insert(0, "FirstItem");
 
        foreach (string s in list) {
            Console.WriteLine(s);
        }
    }
}
 
 
/*
run:
       
FirstItem
c#
java
c
c++
 
*/

 



answered Aug 3, 2024 by avibootz

Related questions

1 answer 126 views
1 answer 170 views
1 answer 124 views
1 answer 170 views
1 answer 225 views
1 answer 151 views
1 answer 153 views
...