Interviews2 answers
How to use Dictionary class in Systems.Collections.Generic?
2 Answers
// Create a new dictionary of strings, with string keys.
//
Dictionary dictOpenWith =
new Dictionary();
// Add some elements to the dictionary. There are no
// duplicate keys, but some of the values are duplicates.
dictOpenWith.Add("txt", "notepad.exe");
dictOpenWith.Add("bmp", "paint.exe");
dictOpenWith.Add("dib", "paint.exe");
dictOpenWith.Add("rtf", "wordpad.exe");
Dnyaneshwar KawathePosted Jul 28, 2014, 5:14 AM EDT
Create a Dictionary object and populate it and enumerate it to display key value pairs.
For example, to create a Dictionary object that has integer as key and string as value, we can create dictionary object in the following way:
Dictionary
dictNames.Add(1,”Raj”);
dictNames.Add(3,”Ravi”);
dictNames.Add(5,”Kevin”);
In order to retrieve above value enumerate it i.e use foreach loop and use KeyValuePair object as the elements are retrieved as KeyValuePair objects
foreach(KeyValuePair
Console.WriteLine(kvpName.Key+” “+kvpName.Value);
Yogesh JadhavPosted Sep 23, 2013, 1:09 AM EDT

