Dictionary Class in C#

Dictionary in C# is a generic collection class that stores key-value pairs. It belongs to the System.Collections.Generic namespace and provides fast lookups based on keys. Each key in the dictionary must be unique, but values can be duplicated.

Syntax

Following is the syntax for declaring a Dictionary −

public class Dictionary<TKey, TValue>

Where TKey is the type of keys and TValue is the type of values in the dictionary.

To create and initialize a Dictionary −

Dictionary<TKey, TValue> dict = new Dictionary<TKey, TValue>();
dict.Add(key, value);
dict[key] = value;  // Alternative way to add/update

Creating and Adding Elements

The following example demonstrates how to create a Dictionary and add elements using the Add() method −

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main() {
      Dictionary<string, string> dict = new Dictionary<string, string>();
      dict.Add("One", "John");
      dict.Add("Two", "Tom");
      dict.Add("Three", "Jacob");
      dict.Add("Four", "Kevin");
      dict.Add("Five", "Nathan");
      
      Console.WriteLine("Key/value pairs...");
      foreach(KeyValuePair<string, string> res in dict) {
         Console.WriteLine("Key = {0}, Value = {1}", res.Key, res.Value);
      }
   }
}

The output of the above code is −

Key/value pairs...
Key = One, Value = John
Key = Two, Value = Tom
Key = Three, Value = Jacob
Key = Four, Value = Kevin
Key = Five, Value = Nathan

Removing Elements and Accessing Keys

The following example shows how to remove elements using Remove() method and access all keys using the 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("Removing some keys...");
      dict.Remove("Four");
      dict.Remove("Five");
      Console.WriteLine("Count of elements (updated) = " + 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
Removing some keys...
Count of elements (updated) = 3

Key/value pairs...
Key = One, Value = Kagido
Key = Two, Value = Ngidi
Key = Three, Value = Devillers

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

Common Dictionary Operations

Here are some frequently used Dictionary methods and properties −

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main() {
      Dictionary<int, string> dict = new Dictionary<int, string>();
      
      // Adding elements
      dict[1] = "Apple";
      dict[2] = "Banana";
      dict[3] = "Cherry";
      
      // Check if key exists
      if (dict.ContainsKey(2)) {
         Console.WriteLine("Key 2 exists: " + dict[2]);
      }
      
      // Check if value exists
      if (dict.ContainsValue("Apple")) {
         Console.WriteLine("Apple found in dictionary");
      }
      
      // Try to get value safely
      string value;
      if (dict.TryGetValue(4, out value)) {
         Console.WriteLine("Key 4: " + value);
      } else {
         Console.WriteLine("Key 4 not found");
      }
      
      // Clear all elements
      Console.WriteLine("Count before clear: " + dict.Count);
      dict.Clear();
      Console.WriteLine("Count after clear: " + dict.Count);
   }
}

The output of the above code is −

Key 2 exists: Banana
Apple found in dictionary
Key 4 not found
Count before clear: 3
Count after clear: 0

Conclusion

Dictionary in C# is a powerful collection that provides fast key-based lookups using hash tables. It supports adding, removing, and searching for key-value pairs efficiently, making it ideal for scenarios where you need to associate unique keys with specific values.

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

992 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements