c# initialize dictionary
Dictionary<int, string> dictNew = new Dictionary<int,string>()
{
{1, "true"},
{2, "false"}
}
c# initialize dictionary
Dictionary<int, string> dictNew = new Dictionary<int,string>()
{
{1, "true"},
{2, "false"}
}
c# inline initialize dictionary
var testDict = new Dictionary<string, string>() { ["key1"] = "value1", ["key2"] = "value2" };
c sharp create dictionary
// To initialize a dictionary, see below:
IDictionary<int, string> dict = new Dictionary<int, string>();
// Make sure to give it the right type of key and value
// To add values use 'Add()'
dict.Add(1,"One");
dict.Add(2,"Two");
dict.Add(3,"Three");
// You can also do this together with the creation
IDictionary<int, string> dict = new Dictionary<int, string>()
{
{1,"One"},
{2, "Two"},
{3,"Three"}
};
declare dictionary c#
var students2 = new Dictionary<int, StudentName>()
{
[111] = new StudentName { FirstName="Sachin", LastName="Karnik", ID=211 },
[112] = new StudentName { FirstName="Dina", LastName="Salimzianova", ID=317 } ,
[113] = new StudentName { FirstName="Andy", LastName="Ruth", ID=198 }
};
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);
}
}
}
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us