How to create a Dictionary using C#?

A Dictionary in C# is a collection of key-value pairs where each key is unique. The Dictionary<TKey, TValue> class is part of the System.Collections.Generic namespace and provides fast lookups based on keys.

Dictionaries are useful when you need to associate values with unique identifiers, such as storing employee IDs with names or product codes with prices.

Syntax

Following is the syntax for creating a Dictionary −

Dictionary<TKey, TValue> dictionaryName = new Dictionary<TKey, TValue>();

Following is the syntax for adding items to a Dictionary −

dictionaryName.Add(key, value);
dictionaryName[key] = value;  // Alternative syntax

Using Dictionary with Add() Method

The Add() method is used to insert key-value pairs into the Dictionary. If you try to add a duplicate key, it will throw an exception −

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main() {
      Dictionary<int, int> numbers = new Dictionary<int, int>();
      
      numbers.Add(1, 44);
      numbers.Add(2, 34);
      numbers.Add(3, 66);
      numbers.Add(4, 47);
      numbers.Add(5, 76);

      Console.WriteLine("Dictionary Count: " + numbers.Count);
      
      foreach (KeyValuePair<int, int> pair in numbers) {
         Console.WriteLine("Key: " + pair.Key + ", Value: " + pair.Value);
      }
   }
}

The output of the above code is −

Dictionary Count: 5
Key: 1, Value: 44
Key: 2, Value: 34
Key: 3, Value: 66
Key: 4, Value: 47
Key: 5, Value: 76

Using Dictionary with Index Notation

You can also use index notation to add or update values in a Dictionary. This approach will not throw an exception if the key already exists −

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main() {
      Dictionary<string, string> countries = new Dictionary<string, string>();
      
      countries["US"] = "United States";
      countries["UK"] = "United Kingdom";
      countries["IN"] = "India";
      countries["CA"] = "Canada";
      
      Console.WriteLine("Countries in Dictionary:");
      foreach (var country in countries) {
         Console.WriteLine(country.Key + " = " + country.Value);
      }
      
      // Update existing value
      countries["US"] = "United States of America";
      Console.WriteLine("\nAfter update:");
      Console.WriteLine("US = " + countries["US"]);
   }
}

The output of the above code is −

Countries in Dictionary:
US = United States
UK = United Kingdom
IN = India
CA = Canada

After update:
US = United States of America

Using Collection Initializer Syntax

C# provides a convenient way to initialize a Dictionary with values at the time of creation −

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main() {
      Dictionary<int, string> fruits = new Dictionary<int, string>() {
         {1, "Apple"},
         {2, "Banana"},
         {3, "Orange"},
         {4, "Mango"}
      };
      
      Console.WriteLine("Fruits Dictionary:");
      foreach (var fruit in fruits) {
         Console.WriteLine("ID: " + fruit.Key + ", Name: " + fruit.Value);
      }
      
      // Check if key exists
      if (fruits.ContainsKey(3)) {
         Console.WriteLine("\nFruit with ID 3: " + fruits[3]);
      }
   }
}

The output of the above code is −

Fruits Dictionary:
ID: 1, Name: Apple
ID: 2, Name: Banana
ID: 3, Name: Orange
ID: 4, Name: Mango

Fruit with ID 3: Orange

Common Dictionary Operations

Method Description
Add(key, value) Adds a key-value pair (throws exception if key exists)
ContainsKey(key) Returns true if the key exists
Remove(key) Removes the key-value pair
TryGetValue(key, out value) Safely gets value without throwing exception
Clear() Removes all key-value pairs

Conclusion

Creating a Dictionary in C# is straightforward using the Dictionary<TKey, TValue> class. You can add items using the Add() method or index notation, and initialize values using collection initializer syntax. Dictionaries provide efficient key-based lookups and are essential for many programming scenarios.

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

289 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements