How to use Dictionary class in Systems.Collections.Generic

 Posted by deccansoft on 9/27/2010 | Category: C# Interview questions | Views: 1200

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>();




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 

Comments or Responses

Login to post response