Create a Dictionary object and populate it and enumerate it to display key value pairs.
Example if we want to have integer as key and string as value. we can create dictionary object in the following way:
Dictionary<int,string> dictNames =new Dictionary<int,string>();
dicNames.Add(1,”Raj”);
dicNames.Add(3,”Ravi”);
dicNames.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<int,string> kvpName in dictNames)
Console.WriteLine(kvpName.Key+” “+kvpName.Value);
Asked In: Many Interviews |
Alert Moderator