C# | Dictionary.Remove Method Last Updated : 01 Feb, 2019 Summarize Comments Improve Suggest changes Share Like Article Like Report This method is used to remove the value with the specified key from the Dictionary<TKey,TValue>. Syntax: public bool Remove (TKey key); Return Value: This method returns true if the element is successfully found and removed; otherwise it returns false. This method returns false if key is not found in the Dictionary. Exception: This method will give ArgumentNullException if the key is null. Below are the programs to illustrate the use of above-discussed method: Example 1: csharp // C# code to remove the entry with // the specified key from the Dictionary using System; using System.Collections.Generic; class GFG { // Driver code public static void Main() { // Create a new dictionary of // strings, with string keys. Dictionary<string, string> myDict = new Dictionary<string, string>(); // Adding key/value pairs in myDict myDict.Add("Australia", "Canberra"); myDict.Add("Belgium", "Brussels"); myDict.Add("Netherlands", "Amsterdam"); myDict.Add("China", "Beijing"); myDict.Add("Russia", "Moscow"); myDict.Add("India", "New Delhi"); // To get count of key/value pairs in myDict Console.WriteLine("Total key/value pairs"+ " in myDict are : " + myDict.Count); // Remove the entry with the specified // key from the Dictionary myDict.Remove("Russia"); Console.WriteLine("After remove operation"); // To get count of key/value pairs in myDict Console.WriteLine("Total key/value pairs"+ " in myDict are : " + myDict.Count); } } Output: Total key/value pairs in myDict are : 6 After remove operation Total key/value pairs in myDict are : 5 Example 2: csharp // C# code to remove the entry with // the specified key from the Dictionary using System; using System.Collections.Generic; class GFG { // Driver code public static void Main() { // Create a new dictionary of // strings, with string keys. Dictionary<int, int> myDict = new Dictionary<int, int>(); // Adding key/value pairs in myDict myDict.Add(9, 8); myDict.Add(3, 4); myDict.Add(4, 7); myDict.Add(1, 7); // To get count of key/value pairs in myDict Console.WriteLine("Total key/value pairs "+ "in myDict are : " + myDict.Count); // Remove the entry with the specified // key from the Dictionary myDict.Remove(9); Console.WriteLine("After remove operation"); // To get count of key/value pairs in myDict Console.WriteLine("Total key/value pairs in"+ " myDict are : " + myDict.Count); } } Output: Total key/value pairs in myDict are : 4 After remove operation Total key/value pairs in myDict are : 3 Reference: https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2.remove?view=netframework-4.7.2 Comment More infoAdvertise with us Next Article C# | Dictionary.Remove Method R rupesh_rao Follow Improve Article Tags : C# CSharp-method CSharp-Generic-Namespace CSharp Dictionary Class Similar Reads C# | Dictionary.Clear Method This method is used to remove all key/value pairs from the Dictionary<TKey,TValue>. Syntax: public void Clear (); Below are the programs to illustrate the use of above-discussed method: Example 1: csharp // C# code to remove all pairs // from Dictionary using System; using System.Collections.G 2 min read C# String Remove() Method In C#, the Remove() method of the String class is used for removing the characters from the specified position of a string. If the length is not specified, then it will remove all the characters after the specified position. This method can be overloaded by changing the number of arguments passed to 3 min read C# | SortedDictionary.Clear() Method This method is used to remove all key/value pairs from the SortedDictionary<TKey, TValue>. Syntax: public void Clear (); Below are the programs to illustrate the use of the above-discussed method: Example 1: csharp // C# code to remove all pairs // from SortedDictionary using System; using Sys 2 min read C# | Remove all entries from the ListDictionary ListDictionary.Clear method is used to remove the all entries from the ListDictionary. Syntax: public void Clear (); Below given are some examples to understand the implementation in a better way: Example 1: CSHARP // C# code to remove all entries // from the ListDictionary using System; using Syste 3 min read C# | Remove all elements from OrderedDictionary OrderedDictionary.Clear method is used to remove all elements from the OrderedDictionary collection. Syntax: public void Clear (); Exception: If the OrderedDictionary collection is read-only then it will throw NotSupportedException. Below given are some examples to understand the implementation in a 2 min read Like