Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C# program to get the List of keys from a Dictionary
In C#, you can extract all the keys from a Dictionary as a List using the Keys property. This is useful when you need to work with dictionary keys as a separate collection or perform operations like sorting, filtering, or iteration.
Syntax
Following is the syntax to get keys from a Dictionary −
Dictionary<TKey, TValue> dictionary = new Dictionary<TKey, TValue>(); List<TKey> keys = new List<TKey>(dictionary.Keys);
You can also use LINQ to convert keys to a List −
List<TKey> keys = dictionary.Keys.ToList();
Using Dictionary.Keys Property
The Keys property returns a collection of all keys in the dictionary. We can convert this collection to a List for easier manipulation −
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);
}
}
}
The output of the above code is −
Displaying keys... 1 2 3 4 5 6 7 8
Using LINQ ToList() Method
You can also use LINQ's ToList() method to convert the keys collection directly to a List −
using System;
using System.Collections.Generic;
using System.Linq;
public class Demo {
public static void Main() {
Dictionary<string, int> scores = new Dictionary<string, int>() {
{"Alice", 95},
{"Bob", 87},
{"Charlie", 92},
{"Diana", 98}
};
// getting keys using LINQ
List<string> playerNames = scores.Keys.ToList();
Console.WriteLine("Player Names:");
foreach (string name in playerNames) {
Console.WriteLine("- " + name);
}
Console.WriteLine("\nTotal Players: " + playerNames.Count);
}
}
The output of the above code is −
Player Names: - Alice - Bob - Charlie - Diana Total Players: 4
Working with Different Key Types
This approach works with any key type. Here's an example using string keys and custom object values −
using System;
using System.Collections.Generic;
public class Product {
public string Name { get; set; }
public double Price { get; set; }
}
public class Demo {
public static void Main() {
Dictionary<string, Product> inventory = new Dictionary<string, Product>();
inventory.Add("P001", new Product { Name = "Laptop", Price = 999.99 });
inventory.Add("P002", new Product { Name = "Mouse", Price = 25.50 });
inventory.Add("P003", new Product { Name = "Keyboard", Price = 75.00 });
// getting product codes (keys)
List<string> productCodes = new List<string>(inventory.Keys);
Console.WriteLine("Product Codes in Inventory:");
foreach (string code in productCodes) {
Console.WriteLine(code);
}
}
}
The output of the above code is −
Product Codes in Inventory: P001 P002 P003
Comparison of Methods
| Method | Syntax | Performance | Requirements |
|---|---|---|---|
| Constructor | new List<T>(dict.Keys) |
Slightly faster | No additional namespace |
| LINQ ToList() | dict.Keys.ToList() |
Slightly slower | Requires using System.Linq;
|
Conclusion
Getting keys from a Dictionary in C# is straightforward using either the List constructor with dictionary.Keys or LINQ's ToList() method. Both approaches create a separate List that you can manipulate independently from the original Dictionary. The constructor approach is slightly more efficient and doesn't require additional namespace imports.
