How to initialize a dictionary with values in C#?
In the following code example, a Dictionarystring
.using System;
using System.Collections.Generic;
public class Example
{
public static void Main()
{
var myDict = new Dictionary<string, string>
{
{ "key1", "value1" },
{ "key2", "value2" } // You can more items after this element
};
Console.WriteLine(myDict["key1"]); // prints value1
Console.WriteLine(myDict["key2"]); // prints value2
}
}
Output
value1
value2
value2