Dictionary.Add() Method in C#

The Dictionary.Add() method in C# is used to add a specified key-value pair to the dictionary. This method adds elements to the dictionary and throws an exception if you try to add a duplicate key.

Syntax

Following is the syntax −

public void Add(TKey key, TValue value)

Parameters

  • key − The key of the element to add. Cannot be null.

  • value − The value of the element to add. Can be null for reference types.

Return Value

This method does not return any value. It throws an ArgumentException if the key already exists in the dictionary.

Using Dictionary.Add() with String Keys

Let us see an example of adding key-value pairs to a 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", "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

Using Dictionary.Add() to Track Count

You can use the Count property to track how many elements have been added −

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("Count of elements = " + dict.Count);
        
        dict.Add("Six", "Anne");
        dict.Add("Seven", "Katie");
        
        Console.WriteLine("Count of elements (updated) = " + dict.Count);
        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 −

Count of elements = 5
Count of elements (updated) = 7
Key/value pairs...
Key = One, Value = John
Key = Two, Value = Tom
Key = Three, Value = Jacob
Key = Four, Value = Kevin
Key = Five, Value = Nathan
Key = Six, Value = Anne
Key = Seven, Value = Katie

Handling Duplicate Keys

The Add() method throws an exception when trying to add a duplicate key. Here's how to handle this scenario −

using System;
using System.Collections.Generic;

public class Demo {
    public static void Main() {
        Dictionary<int, string> dict = new Dictionary<int, string>();
        
        dict.Add(1, "Apple");
        dict.Add(2, "Banana");
        
        Console.WriteLine("Initial dictionary:");
        foreach(var item in dict) {
            Console.WriteLine("Key: {0}, Value: {1}", item.Key, item.Value);
        }
        
        try {
            dict.Add(1, "Orange"); // This will throw an exception
        }
        catch (ArgumentException ex) {
            Console.WriteLine("Error: " + ex.Message);
        }
        
        // Safe way to add - check if key exists first
        if (!dict.ContainsKey(3)) {
            dict.Add(3, "Cherry");
            Console.WriteLine("Added Cherry successfully");
        }
    }
}

The output of the above code is −

Initial dictionary:
Key: 1, Value: Apple
Key: 2, Value: Banana
Error: An item with the same key has already been added.
Added Cherry successfully

Conclusion

The Dictionary.Add() method is essential for adding key-value pairs to a dictionary in C#. Remember that it throws an exception for duplicate keys, so use ContainsKey() to check for existing keys or consider using the indexer syntax dict[key] = value for safer key assignment.

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

377 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements