Following is the syntax for declaring a dictionary in C# :
Dictionary<int,string> NewDictionary = new Dictionary<int,string>();
Now elements can be added in this list using function :
NewDictionary.Add(1,“Name1”);
NewDictionary.Add(2,“Name2”);
Here 1 and 2 represents unique keys for vaules ‘Name1’ and ‘Name2’. The key is a unique value and same keys cannot exist in a dictionary.
For Example the below code will give a error because the key 1 is duplicated in the dictionary:
NewDictionary.Add(1,“Name1”);
NewDictionary.Add(1,“Name2”);
The image below shows the exception that occurs due to duplicate keys :