How to add an element to a list in C#

1 Answer

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

class Program
{
    public static void PrintList(List<int> lst) {
        foreach (int n in lst) {
            Console.Write(n + " ");
        }
    }
    static void Main() {
        List<int> lst = new List<int>() { 3, 5, 6, 8 };

        PrintList(lst);
        
        lst.Add(1);
        
        Console.WriteLine();
        PrintList(lst);
    }
}





/*
run:

3 5 6 8 
3 5 6 8 1 

*/

 



answered Aug 16, 2021 by avibootz

Related questions

1 answer 52 views
1 answer 110 views
110 views asked Aug 16, 2021 by avibootz
1 answer 155 views
155 views asked Aug 16, 2021 by avibootz
1 answer 109 views
1 answer 116 views
116 views asked Jul 23, 2023 by avibootz
1 answer 153 views
1 answer 74 views
...