- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C# program to get the List of keys from a Dictionary
Set the dictionary elements −
Dictionary<int, string> d = new Dictionary<int, string>(); // dictionary elements d.Add(1, "One"); d.Add(2, "Two"); d.Add(3, "Three"); d.Add(4, "Four"); d.Add(5, "Five"); d.Add(6, "Six"); d.Add(7, "Seven"); d.Add(8, "Eight");
To get the keys, use a list collection −
List<int> keys = new List<int>(d.Keys);
Loop through the keys and display them.
Here is the complete code −
Example
using System; using System.Collections.Generic; public class Demo { public static void Main() { Dictionary<int, string> d = new Dictionary<int, string>(); // dictionary elements d.Add(1, "One"); d.Add(2, "Two"); d.Add(3, "Three"); d.Add(4, "Four"); d.Add(5, "Five"); d.Add(6, "Six"); d.Add(7, "Seven"); d.Add(8, "Eight"); // getting keys List<int> keys = new List<int>(d.Keys); Console.WriteLine("Displaying keys..."); foreach (int res in keys) { Console.WriteLine(res); } } }
Output
Displaying keys... 1 2 3 4 5 6 7 8
- Related Articles
- How to get a list of all the keys from a Python dictionary?
- Get dictionary keys as a list in Python
- Python Program to get all unique keys from a List of Dictionaries
- C# Program to combine Dictionary of two keys
- How to create Python dictionary from list of keys and values?
- C++ Program to Sort Dictionary by keys
- Python – Limit the values to keys in a Dictionary List
- How to get a list of all the values from a Python dictionary?
- Python program to find the sum of dictionary keys
- Python program to get maximum of each key Dictionary List
- How to access nested Python dictionary items via a list of keys?
- Python Program – Create dictionary from the list
- How to get keys from a HashTable in C#?
- C# Program to get the first three elements from a list
- Python Extract specific keys from dictionary?

Advertisements