This post will discuss how to remove a key from a Dictionary in C#.

1. Using Dictionary<TKey,TValue>.Remove() Method

The standard solution to remove a value with the specified key from a dictionary is using the Dictionary<TKey,TValue>.Remove() method. The following example uses the Remove() method to remove a key-value pair from a dictionary.

Download  Run Code

 
The Remove() method returns true if the key is successfully removed from the dictionary and false if the key is not found in the dictionary, as the following example illustrates.

Download  Run Code

2. Using LINQ

To avoid any modifications to the existing dictionary, you can use LINQ to construct a new dictionary instance without the desired key-value pairs. The following code example shows how to remove the specified collection of keys from a dictionary.

Download  Run Code

That’s all about removing a key from a Dictionary in C#.