Dictionary.Keys Property in C#

The Dictionary.Keys property in C# is used to fetch all the keys in the Dictionary. This property returns a KeyCollection that contains all the keys from the dictionary, which can be iterated through using a foreach loop.

Syntax

Following is the syntax −

public System.Collections.Generic.Dictionary<TKey, TValue>.KeyCollection Keys { get; }

Return Value

The Keys property returns a Dictionary<TKey, TValue>.KeyCollection containing all the keys in the dictionary. This collection is a live view of the keys, meaning if the dictionary changes, the collection reflects those changes.

Using Dictionary.Keys Property

Example

Let us see an example to implement the Dictionary.Keys property −

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main() {
      Dictionary<string, string> dict = new Dictionary<string, string>();
      dict.Add("One", "Kagido");
      dict.Add("Two", "Ngidi");
      dict.Add("Three", "Devillers");
      dict.Add("Four", "Smith");
      dict.Add("Five", "Warner");
      
      Console.WriteLine("Count of elements = " + dict.Count);
      Console.WriteLine("\nKey/value pairs...");
      
      foreach(KeyValuePair<string, string> res in dict) {
         Console.WriteLine("Key = {0}, Value = {1}", res.Key, res.Value);
      }
      
      Console.Write("\nAll the keys..<br>");
      Dictionary<string, string>.KeyCollection allKeys = dict.Keys;
      
      foreach(string str in allKeys) {
         Console.WriteLine("Key = {0}", str);
      }
   }
}

The output of the above code is −

Count of elements = 5

Key/value pairs...
Key = One, Value = Kagido
Key = Two, Value = Ngidi
Key = Three, Value = Devillers
Key = Four, Value = Smith
Key = Five, Value = Warner

All the keys..
Key = One
Key = Two
Key = Three
Key = Four
Key = Five

Using Keys Collection with LINQ Operations

Example

Let us see how to perform operations on the keys collection −

using System;
using System.Collections.Generic;
using System.Linq;

public class Demo {
   public static void Main() {
      Dictionary<int, string> dict = new Dictionary<int, string>();
      dict.Add(1, "Apple");
      dict.Add(5, "Banana");
      dict.Add(3, "Cherry");
      dict.Add(8, "Date");
      dict.Add(2, "Elderberry");
      
      Console.WriteLine("All keys:");
      foreach(int key in dict.Keys) {
         Console.WriteLine("Key: " + key);
      }
      
      Console.WriteLine("\nKeys greater than 3:");
      var largeKeys = dict.Keys.Where(k => k > 3);
      foreach(int key in largeKeys) {
         Console.WriteLine("Key: " + key + ", Value: " + dict[key]);
      }
      
      Console.WriteLine("\nMaximum key: " + dict.Keys.Max());
      Console.WriteLine("Minimum key: " + dict.Keys.Min());
   }
}

The output of the above code is −

All keys:
Key: 1
Key: 5
Key: 3
Key: 8
Key: 2

Keys greater than 3:
Key: 5, Value: Banana
Key: 8, Value: Date

Maximum key: 8
Minimum key: 1

Keys Collection with Dictionary Updates

Example

Let us see how the keys collection reflects changes in the dictionary −

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main() {
      Dictionary<string, string> dict = new Dictionary<string, string>();
      dict.Add("One", "Chris");
      dict.Add("Two", "Steve");
      dict.Add("Three", "Messi");
      
      Console.WriteLine("Initial keys count: " + dict.Keys.Count);
      foreach(string key in dict.Keys) {
         Console.WriteLine("Key: " + key);
      }
      
      // Update value (keys remain same)
      dict["Three"] = "Katie";
      Console.WriteLine("\nAfter updating value for 'Three':");
      Console.WriteLine("Keys count: " + dict.Keys.Count);
      
      // Add new key-value pair
      dict.Add("Four", "Ryan");
      Console.WriteLine("\nAfter adding new key 'Four':");
      Console.WriteLine("Keys count: " + dict.Keys.Count);
      foreach(string key in dict.Keys) {
         Console.WriteLine("Key: " + key + ", Value: " + dict[key]);
      }
   }
}

The output of the above code is −

Initial keys count: 3
Key: One
Key: Two
Key: Three

After updating value for 'Three':
Keys count: 3

After adding new key 'Four':
Keys count: 4
Key: One, Value: Chris
Key: Two, Value: Steve
Key: Three, Value: Katie
Key: Four, Value: Ryan

Conclusion

The Dictionary.Keys property provides a convenient way to access all keys in a dictionary as a collection. This collection is a live view that automatically reflects changes made to the dictionary, making it useful for iteration, filtering, and other key-based operations.

Updated on: 2026-03-17T07:04:35+05:30

389 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements