dictionary int list c# code example
Example 1: how to create dictionary of list in c#
public static void Main()
{
Dictionary<String, List<string>> dic = new Dictionary<string, List<string>>();
List<string> li1 = new List<string>();
li1.Add("text1"); li1.Add("text2"); li1.Add("text3"); li1.Add("text4");
List<string> li2 = new List<string>();
li2.Add("text1"); li2.Add("text2"); li2.Add("text3"); li2.Add("text4");
dic["1"] = li1;
dic["2"] = li2;
foreach (string key in dic.Keys)
{
foreach (string val in dic[key])
{
Console.WriteLine(val);
}
}
}
Example 2: dictionary string list int c#
Dictionary<string, List<int>> dictionaryStringListOfInt = new Dictionary<string, List<int>>(){
{ "The First", new List<int>(){ 1,2 } },
{ "The Second", new List<int>(){ 1,2 } }
};
dictionaryStringListOfInt.Add("The New", new List<int>());
dictionaryStringListOfInt["The New"].Add(1);
dictionaryStringListOfInt["The New"].Add(2);
Console.WriteLine(dictionaryStringListOfInt["The New"][0]);
dictionaryStringListOfInt["The New"][0] = 3;
Console.WriteLine(dictionaryStringListOfInt["The New"][0]);
Console.WriteLine(dictionaryStringListOfInt["The New"][1]);