C# Dictionary Remove() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Dictionary

Definition

In C#, Dictionary.Remove(key) deletes the pair that has the given key. It returns true when the key was found and removed, and false when the key was not in the dictionary.

A missing key is not an error, so you do not need to check ContainsKey first. There is also a second overload, Remove(key, out value), which hands you the value that was removed.

This is an instance member of Dictionary<TKey,TValue> (System.Collections.Generic). Call it on a dictionary variable, such as ages.Remove(...). A dictionary is a class, so two variables can share one dictionary. The overload Remove(key, out value) exists in .NET Core 2.0 and later, .NET Standard 2.1, and .NET 5 and later. It is not in .NET Framework.

Syntax

Pass the key you want to delete. Use the second form if you also want the old value.

C#
Dictionary<string, int> ages = new Dictionary<string, int>();
ages.Add("Asha", 25);
ages.Add("Ravi", 31);

bool a = ages.Remove("Ravi");                 // true
bool b = ages.Remove("Zed");                  // false
bool c = ages.Remove("Asha", out int old);    // true, old is 25
CallWhat it does
ages.Remove("Ravi")Deletes the pair and returns true. Returns false if "Ravi" is not a key.
ages.Remove("Asha", out int old)Same removal, and old receives the removed value (or the default if the key was missing).
ages.Clear()Removes all pairs at once. See Clear().

A null key throws ArgumentNullException. After a successful removal, Count goes down by 1.

Library declarations
C#
public bool Remove(TKey key);
public bool Remove(TKey key, out TValue value);

Parameters

ParameterDescription
keyRequired. The key of the pair to remove. It must not be null.
valueOutput, second overload only. Receives the removed value, or the default value of TValue when the key was not found.

Return Value

Return ValueDescription
booltrue if the pair was found and removed; false if the key was not in the dictionary.

C# Examples

Example 1 — Remove One Existing and One Missing Key

Remove a name that is stored and a name that is not, then check the dictionary.

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.Remove("Ravi"));
        Console.WriteLine(ages.Remove("Zed"));
        Console.WriteLine(ages.Count);
        Console.WriteLine(ages.ContainsKey("Ravi"));
    }
}

How It Works

  1. Found key. "Ravi" is stored, so Remove deletes the pair and returns True.
  2. Missing key. "Zed" was never added, so Remove returns False and nothing happens. There is no exception.
  3. Count drops once. Three pairs became two, because only one removal worked.
  4. Key is gone. ContainsKey("Ravi") is now False.

Example 2 — Remove and Keep the Old Value

Use the Remove(key, out value) overload to take a value out of the dictionary in one step.

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);

        bool removed = ages.Remove("Ravi", out int oldAge);
        Console.WriteLine(removed);
        Console.WriteLine(oldAge);

        bool again = ages.Remove("Zed", out int none);
        Console.WriteLine(again);
        Console.WriteLine(none);
        Console.WriteLine(ages.Count);
    }
}

How It Works

  1. Two results at once. The call returns a bool and also fills oldAge through the out parameter.
  2. Old value captured. oldAge is 31, the value that belonged to "Ravi" before it was deleted.
  3. Missing key. For "Zed", the result is False and none gets the default 0.
  4. One pair left. Only "Ravi" was removed, so "Asha" remains and Count is 1.

Example 3 — Remove All Entries Under 30

Collect the keys in a list first, then remove them after the loop ends.

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);
        ages.Add("Kiran", 40);

        List<string> toRemove = new List<string>();
        foreach (KeyValuePair<string, int> pair in ages) {
            if (pair.Value < 30) {
                toRemove.Add(pair.Key);
            }
        }

        foreach (string key in toRemove) {
            ages.Remove(key);
        }

        Console.WriteLine(toRemove.Count);
        Console.WriteLine(ages.Count);
        Console.WriteLine(ages.ContainsKey("Asha"));
        Console.WriteLine(ages.ContainsKey("Ravi"));
    }
}

How It Works

  1. First loop only reads. It does not change the dictionary. It copies the keys of everyone under 30 into toRemove.
  2. Two keys collected. "Asha" (25) and "Meena" (28) match, so toRemove.Count is 2. The list order depends on the dictionary order, but the total does not.
  3. Second loop removes. It walks the list, not the dictionary, so there is nothing to confuse the enumerator.
  4. Result. Two pairs remain, "Asha" is gone, and "Ravi" (31) is still there.

Frequently Asked Questions

It returns true if the key was found and the pair was removed, and false if the key was not in the dictionary.
No. A missing key just returns false. Only a null key throws ArgumentNullException.
Use the overload Remove(key, out value). It is available in .NET Core 2.0 and later and .NET 5 and later.
Avoid it. It can throw InvalidOperationException. Collect the keys into a List first and remove them after the loop.
Remove deletes one pair by key. Clear deletes every pair and sets Count to 0.

Next: TrimExcess()

Shrink the Dictionary's reserved space to fit its current number of entries.

TrimExcess() 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.

13 people found this page helpful