In C#, Dictionary.Add(key, value) puts a new key and value pair into a dictionary. A dictionary works like a phone book: you look up a name (the key) and get a number (the value).
Every key must be unique. If the key is already in the dictionary, Add does not replace it. It throws an exception instead. Use TryAdd() or the indexer when you do not want an exception.
This is an instance member of Dictionary<TKey,TValue> (System.Collections.Generic). Call it on a dictionary variable, such as ages.Add(...). A dictionary is a class, so two variables can share one dictionary.
Syntax
Call Add on a dictionary variable. Pass the key first and the value second.
C#
Dictionary<string, int> ages = new Dictionary<string, int>();
ages.Add("Asha", 25); // new key: OK
ages.Add("Ravi", 31); // new key: OK
// ages.Add("Asha", 26); // same key again: ArgumentException
Call
What it does
ages.Add("Asha", 25)
Adds a new pair. Throws ArgumentException if "Asha" is already a key.
ages["Asha"] = 26
Indexer set: adds the key if it is new, or overwrites the old value. No exception.
ages.TryAdd("Asha", 25)
Returns false instead of throwing when the key already exists.
A null key throws ArgumentNullException. A null value is fine when TValue is a reference type. Add is an average O(1) call.
Library declarations
C#
public void Add(TKey key, TValue value);
Parameters
Parameter
Description
key
Required. The key to add. It must not be null and must not already be in the dictionary.
value
Required. The value linked to the key. It may be null for reference types. Values do not have to be unique.
Return Value
Return Value
Description
void
Nothing is returned. After a successful call, Count goes up by 1.
C# Examples
Example 1 — Add Three Ages and Read Them Back
Create a dictionary of names and ages, add three pairs, then read the result without relying on any order.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
Dictionary<string, int> ages = new Dictionary<string, int>();
ages.Add("Asha", 25);
ages.Add("Ravi", 31);
ages.Add("Meena", 28);
Console.WriteLine(ages.Count);
Console.WriteLine(ages["Ravi"]);
Console.WriteLine(ages.ContainsKey("Meena"));
}
}
Output
3
31
True
How It Works
Empty start.new Dictionary<string, int>() has no pairs yet, so Count is 0.
Three Add calls. Each name is new, so all three calls succeed and Count becomes 3.
Read by key.ages["Ravi"] finds the key and gives its value, 31.
No loop needed. The output uses Count, the indexer, and ContainsKey, so it never depends on enumeration order.
Example 2 — See the Two Exceptions
Try to add the same key twice, then try a null key, and catch both errors.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
Dictionary<string, int> ages = new Dictionary<string, int>();
ages.Add("Asha", 25);
try {
ages.Add("Asha", 26);
} catch (ArgumentException) {
Console.WriteLine("Duplicate key");
}
string noKey = null;
try {
ages.Add(noKey, 40);
} catch (ArgumentNullException) {
Console.WriteLine("Null key");
}
Console.WriteLine(ages["Asha"]);
Console.WriteLine(ages.Count);
}
}
Output
Duplicate key
Null key
25
1
How It Works
First Add works."Asha" is new, so the pair Asha=25 is stored.
Second Add fails. The key "Asha" already exists, so ArgumentException is thrown and the catch block prints a message.
Null key fails. A dictionary needs a real key to find the right slot, so null throws ArgumentNullException.
Nothing was changed. The old value is still 25 and Count is still 1, because a failed Add leaves the dictionary alone.
Example 3 — Skip Names You Already Have
Loop over two arrays and use ContainsKey so a repeated name does not crash the program.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
string[] names = { "Asha", "Ravi", "Asha", "Meena" };
int[] scores = { 90, 85, 70, 95 };
Dictionary<string, int> best = new Dictionary<string, int>();
for (int i = 0; i < names.Length; i++) {
if (best.ContainsKey(names[i])) {
Console.WriteLine("Skipped " + names[i]);
} else {
best.Add(names[i], scores[i]);
}
}
Console.WriteLine(best.Count);
Console.WriteLine(best["Asha"]);
}
}
Output
Skipped Asha
3
90
How It Works
Two parallel arrays.names[i] and scores[i] belong together, so the loop uses one index for both.
Guard first.ContainsKey(names[i]) is true only for the second "Asha", so that pass prints a message and skips Add.
First value wins. The score 70 is never stored. best["Asha"] stays 90 from the first pass.
Three unique names. Four names went in but one was a repeat, so Count is 3.
Frequently Asked Questions
Dictionary.Add(key, value) adds a new key and value pair to the dictionary. The key must be unique and must not be null.
That message comes from ArgumentException. You called Add with a key that is already in the dictionary. Use ContainsKey first, call TryAdd, or set the value with ages[key] = value.
Add throws when the key exists. The indexer ages["Asha"] = 26 adds the key if it is new and overwrites the old value if it exists, and it never throws for a duplicate.
Yes, when TValue is a reference type such as string. The key can never be null.
Do not depend on order. A Dictionary may list items in the order you added them, but that is not guaranteed. Use SortedDictionary for sorted keys.