creating a new dictionary c# code example

Example 1: c# initialize dictionary

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

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

Example 3: c sharp add item to dictionary

// To add an item to a dictionary use 'Add()'
dict.Add(1,"One");