What is a dictionary in C#?

A Dictionary in C# is a generic collection that stores data in key-value pairs. It belongs to the System.Collections.Generic namespace and provides fast lookups based on unique keys.

Each key in a Dictionary must be unique, while values can be duplicated. The Dictionary class implements the IDictionary<TKey, TValue> interface and uses hash tables internally for efficient data retrieval.

Syntax

Following is the syntax for declaring a Dictionary −

Dictionary<KeyType, ValueType> dictionaryName = new Dictionary<KeyType, ValueType>();

You can also use the interface type for declaration −

IDictionary<KeyType, ValueType> dictionaryName = new Dictionary<KeyType, ValueType>();

Creating and Initializing a Dictionary

Basic Declaration and Adding Elements

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main() {
      IDictionary<int, string> students = new Dictionary<int, string>();
      
      // Adding elements using Add method
      students.Add(1, "Alice");
      students.Add(2, "Bob");
      students.Add(3, "Charlie");
      
      Console.WriteLine("Dictionary elements: " + students.Count);
      Console.WriteLine("Student with ID 2: " + students[2]);
   }
}

The output of the above code is −

Dictionary elements: 3
Student with ID 2: Bob

Dictionary Initialization with Collection Initializer

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main() {
      Dictionary<string, int> grades = new Dictionary<string, int>() {
         {"Math", 95},
         {"Science", 88},
         {"English", 92}
      };
      
      Console.WriteLine("Subjects and grades:");
      foreach(var item in grades) {
         Console.WriteLine(item.Key + ": " + item.Value);
      }
   }
}

The output of the above code is −

Subjects and grades:
Math: 95
Science: 88
English: 92

Dictionary Operations

Dictionary Operations Add dict.Add(k,v) Access dict[key] Remove dict.Remove(k) Check ContainsKey Update dict[key] = val Clear dict.Clear() Count dict.Count

Example

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main() {
      Dictionary<string, int> inventory = new Dictionary<string, int>();
      
      // Adding items
      inventory.Add("Apples", 50);
      inventory.Add("Bananas", 30);
      inventory["Oranges"] = 25;  // Alternative way to add
      
      Console.WriteLine("Initial inventory count: " + inventory.Count);
      
      // Checking if key exists
      if (inventory.ContainsKey("Apples")) {
         Console.WriteLine("Apples in stock: " + inventory["Apples"]);
      }
      
      // Updating value
      inventory["Apples"] = 45;
      Console.WriteLine("Updated Apples count: " + inventory["Apples"]);
      
      // Removing an item
      inventory.Remove("Bananas");
      Console.WriteLine("After removing Bananas, count: " + inventory.Count);
      
      // Iterating through dictionary
      Console.WriteLine("Current inventory:");
      foreach(KeyValuePair<string, int> item in inventory) {
         Console.WriteLine(item.Key + ": " + item.Value);
      }
   }
}

The output of the above code is −

Initial inventory count: 3
Apples in stock: 50
Updated Apples count: 45
After removing Bananas, count: 2
Current inventory:
Apples: 45
Oranges: 25

Key Features of Dictionary

Feature Description
Unique Keys Each key must be unique; duplicate keys are not allowed.
Fast Lookup O(1) average time complexity for lookups using hash tables.
Generic Type Strongly typed with specified key and value types.
Mutable Elements can be added, updated, or removed after creation.

Conclusion

Dictionary in C# is a powerful collection for storing key-value pairs with fast lookup capabilities. It provides efficient methods for adding, accessing, updating, and removing elements, making it ideal for scenarios where you need to associate unique keys with specific values.

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

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements