c# dictionary declaration code example

Example 1: c# initialize dictionary

Dictionary<int, string> dictNew = new Dictionary<int,string>()
{
  {1, "true"},
  {2, "false"}
}

Example 2: c# inline initialize dictionary

var testDict = new Dictionary<string, string>() { ["key1"] = "value1", ["key2"] = "value2" };

Example 3: 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 }
        };