C#C
C#3y ago
DiMiaN

✅ How to add data to a list that is outside of Main()?

using System.Collections.Generic;
internal class Program
{
    public static void insertData(string animal, int amount)
    {
        var list = new List<KeyValuePair<string, int>>();
        list.Add(new KeyValuePair<string, int>(animal, amount));
    }
    private static void Main(string[] args)
    {
        insertData("Dog", 4);
        insertData("Cat", 12);

        foreach(var element in list)
        {
            Console.WriteLine(element);
        }
    }
}

I know there are some errors but I can't figure out how to solve this. Also when calling the insertData the list will be resetted every new call?
Was this page helpful?