How to declare and initialize a dictionary in C#?

A Dictionary in C# is a collection that stores key-value pairs, where each key must be unique. It is part of the System.Collections.Generic namespace and provides fast lookups based on keys.

Syntax

Following is the syntax for declaring and initializing a Dictionary −

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

Where TKey is the type of the key and TValue is the type of the value.

You can also use the interface type for more flexibility −

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

Using Dictionary.Add() Method

The Add() method allows you to insert key-value pairs into the dictionary −

using System;
using System.Collections.Generic;

public class Demo {
    public static void Main() {
        IDictionary<int, string> students = new Dictionary<int, string>();
        students.Add(1, "John");
        students.Add(2, "Alice");
        students.Add(3, "Bob");
        students.Add(4, "Sarah");

        Console.WriteLine("Dictionary elements: " + students.Count);
        
        foreach(var pair in students) {
            Console.WriteLine("ID: " + pair.Key + ", Name: " + pair.Value);
        }
    }
}

The output of the above code is −

Dictionary elements: 4
ID: 1, Name: John
ID: 2, Name: Alice
ID: 3, Name: Bob
ID: 4, Name: Sarah

Using Collection Initializer

You can initialize a dictionary with values at the time of declaration using collection initializer syntax −

using System;
using System.Collections.Generic;

public class Demo {
    public static void Main() {
        Dictionary<string, int> scores = new Dictionary<string, int>() {
            {"Math", 95},
            {"Science", 87},
            {"English", 92},
            {"History", 88}
        };

        Console.WriteLine("Subject scores:");
        foreach(var item in scores) {
            Console.WriteLine(item.Key + ": " + item.Value);
        }
        
        Console.WriteLine("Total subjects: " + scores.Count);
    }
}

The output of the above code is −

Subject scores:
Math: 95
Science: 87
English: 92
History: 88
Total subjects: 4

Using Index Notation

Dictionary supports index notation for both adding and accessing elements −

using System;
using System.Collections.Generic;

public class Demo {
    public static void Main() {
        Dictionary<int, string> colors = new Dictionary<int, string>();
        
        // Adding elements using index notation
        colors[1] = "Red";
        colors[2] = "Green";
        colors[3] = "Blue";
        
        // Accessing elements
        Console.WriteLine("Color 1: " + colors[1]);
        Console.WriteLine("Color 2: " + colors[2]);
        Console.WriteLine("Color 3: " + colors[3]);
        
        // Modifying existing value
        colors[2] = "Yellow";
        Console.WriteLine("Modified Color 2: " + colors[2]);
    }
}

The output of the above code is −

Color 1: Red
Color 2: Green
Color 3: Blue
Modified Color 2: Yellow

Comparison of Initialization Methods

Method Syntax When to Use
Add() method dict.Add(key, value) Adding elements dynamically, throws exception if key exists
Collection initializer { {key, value}, ... } Known values at declaration time
Index notation dict[key] = value Adding or updating elements, overwrites if key exists

Conclusion

Dictionary in C# can be declared and initialized using several methods including the Add() method, collection initializer syntax, and index notation. Choose the method based on whether you need to add elements at declaration time or dynamically during runtime.

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

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements