dictionary string list int csharp

Code Example - dictionary string list int csharp

                
                        //init
Dictionary<string, List<int>> dictionaryStringListOfInt = new Dictionary<string, List<int>>(){
                { "The First", new List<int>(){ 1,2 } },
                { "The Second", new List<int>(){ 1,2 } }
            };
//add a dictinary Key Value pair with a new empty list
//note you can add an existing list but be carful as it is a ref type and only pointing to ints (changes will be seen in original list ref)
dictionaryStringListOfInt.Add("The New", new List<int>());
//Add some values
dictionaryStringListOfInt["The New"].Add(1);
dictionaryStringListOfInt["The New"].Add(2);
//access some values via the Key and the values index
Console.WriteLine(dictionaryStringListOfInt["The New"][0]);//expected output 1
dictionaryStringListOfInt["The New"][0] = 3;
Console.WriteLine(dictionaryStringListOfInt["The New"][0]);//expected output 3
Console.WriteLine(dictionaryStringListOfInt["The New"][1]);//expected output 2