C# Dictionary Clear() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Dictionary

Definition

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
CallWhat 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

ParameterDescription
NoneClear() takes no parameters.

Return Value

Return ValueDescription
voidNothing 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"));
    }
}

How It Works

  1. Before. Three pairs were added, so the first Count prints 3.
  2. One call. Clear() removes every pair; you do not need a loop.
  3. After. Count is 0, so the dictionary is empty.
  4. 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"]);
    }
}

How It Works

  1. Clear then add. "Asha" is cleared first, and "Ravi" is added afterwards, so only one pair exists.
  2. Old key missing. ContainsKey("Asha") is False, and reading scores["Asha"] throws KeyNotFoundException.
  3. Key can return. Because the old pair was really removed, Add("Asha", 55) does not hit a duplicate-key error.
  4. 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);
    }
}

How It Works

  1. Shared object. b = a does not copy the pairs. Both variables point to the same dictionary in memory.
  2. Clear is seen by both. After a.Clear(), the dictionary is empty, so b.Count prints 0.
  3. New object. c = new Dictionary... makes a second, empty dictionary and moves only c to it.
  4. 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.

Next: ContainsKey()

Check if a key exists in the Dictionary (true or false).

ContainsKey() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful