In C#, Dictionary.Clear() removes all keys and values from a dictionary. After the call, Count is 0 and the dictionary is empty but still usable.
The dictionary object itself is kept, and so is the memory it already reserved (its capacity). Because of that, Clear is a good choice when you want to refill the same dictionary again. Use Remove() to delete a single pair.
This is an instance member of Dictionary<TKey,TValue> (System.Collections.Generic). Call it on a dictionary variable, such as ages.Clear(...). A dictionary is a class, so two variables can share one dictionary.
Syntax
Call Clear with no arguments.
C#
Dictionary<string, int> ages = new Dictionary<string, int>();
ages.Add("Asha", 25);
ages.Add("Ravi", 31);
ages.Clear();
int n = ages.Count; // 0
Call
What it does
ages.Clear()
Deletes every pair. Count becomes 0.
ages = new Dictionary<string, int>()
Does not clear the old object. It points the variable at a brand new empty dictionary.
ages.Remove("Asha")
Deletes only one pair.
Clear takes time proportional to the number of items (O(n)). Use TrimExcess() afterwards if you also want to shrink the memory.
Library declarations
C#
public void Clear();
Parameters
Parameter
Description
None
Clear() takes no parameters.
Return Value
Return Value
Description
void
Nothing is returned. Afterwards Count is 0 and the capacity is unchanged.
C# Examples
Example 1 — Clear a Dictionary and Check It
Add three pairs, clear them, and confirm that the count and the keys are gone.
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);
ages.Clear();
Console.WriteLine(ages.Count);
Console.WriteLine(ages.ContainsKey("Asha"));
}
}
Output
3
0
False
How It Works
Before. Three pairs were added, so the first Count prints 3.
One call.Clear() removes every pair; you do not need a loop.
After.Count is 0, so the dictionary is empty.
Keys gone too.ContainsKey("Asha") is False, because the old pair no longer exists.
Example 2 — Refill the Same Dictionary
After Clear the dictionary works as normal. Old keys are gone and new keys can be added, even ones used before.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
Dictionary<string, int> scores = new Dictionary<string, int>();
scores.Add("Asha", 90);
scores.Clear();
scores.Add("Ravi", 70);
Console.WriteLine(scores.Count);
Console.WriteLine(scores.ContainsKey("Asha"));
try {
Console.WriteLine(scores["Asha"]);
} catch (KeyNotFoundException) {
Console.WriteLine("Asha is gone");
}
scores.Add("Asha", 55);
Console.WriteLine(scores["Asha"]);
}
}
Output
1
False
Asha is gone
55
How It Works
Clear then add."Asha" is cleared first, and "Ravi" is added afterwards, so only one pair exists.
Old key missing.ContainsKey("Asha") is False, and reading scores["Asha"] throws KeyNotFoundException.
Key can return. Because the old pair was really removed, Add("Asha", 55) does not hit a duplicate-key error.
New value. The last line prints 55, not the old 90. Nothing is remembered from before Clear.
Example 3 — Two Variables, One Dictionary
Compare clearing a shared dictionary with pointing one variable at a new dictionary.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
Dictionary<string, int> a = new Dictionary<string, int>();
a.Add("x", 1);
a.Add("y", 2);
Dictionary<string, int> b = a;
a.Clear();
Console.WriteLine(b.Count);
Dictionary<string, int> c = new Dictionary<string, int>();
c.Add("x", 1);
Dictionary<string, int> d = c;
c = new Dictionary<string, int>();
Console.WriteLine(d.Count);
Console.WriteLine(c.Count);
}
}
Output
0
1
0
How It Works
Shared object.b = a does not copy the pairs. Both variables point to the same dictionary in memory.
Clear is seen by both. After a.Clear(), the dictionary is empty, so b.Count prints 0.
New object.c = new Dictionary... makes a second, empty dictionary and moves only c to it.
Old one survives.d still holds the original dictionary with one pair, so it prints 1, while c.Count prints 0.
Frequently Asked Questions
Clear removes all keys and values from the dictionary. Count becomes 0, and you can keep using the same dictionary.
No. The capacity stays the same. Call TrimExcess after Clear if you want to release the extra space.
No. Clear empties the existing object, so every variable that points to it sees an empty dictionary. A new Dictionary leaves the old object alone.
No. Calling Clear on an empty dictionary is fine and does nothing.
Use Remove(key) to delete one pair and Clear() to delete all pairs.