How to get all keys of a dictionary with C#

How to get all keys of a dictionary with C# 

The Keys property gets a collection containing the keys in the Dictionary. It returns an object of KeyCollection type. 

The following code snippet reads all keys in a Dictionary.  

  1. Dictionary<string, Int16> AuthorList = new Dictionary<string, Int16>();  
  2. AuthorList.Add("Mahesh Chand", 35);  
  3. AuthorList.Add("Mike Gold", 25);  
  4. AuthorList.Add("Praveen Kumar", 29);  
  5. AuthorList.Add("Raj Beniwal", 21);  
  6. AuthorList.Add("Dinesh Beniwal", 84);   
  7.   
  8. // Get and display keys  
  9.   
  10. Dictionary<string, Int16>.KeyCollection keys = AuthorList.Keys;  
  11. foreach (string key in keys)  
  12. {  
  13.     Console.WriteLine("Key: {0}", key);  
  14. }  
Output
 
 
 


Similar Articles
Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.