C# | Remove entry with specified key from the StringDictionary Last Updated : 11 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report StringDictionary.Remove(String) method is used to remove the entry with the specified key from the string dictionary. Syntax: public virtual void Remove (string key); Here, key is the key of the entry to remove. Exceptions: ArgumentNullException: If the key is null. NotSupportedException: If the StringDictionary is read-only. Below programs illustrate the use of StringDictionary.Remove(String) method: Example 1: CSHARP // C# code to remove the entry // with the specified key from // the StringDictionary using System; using System.Collections; using System.Collections.Specialized; class GFG { // Driver code public static void Main() { // Creating a StringDictionary named myDict StringDictionary myDict = new StringDictionary(); // Adding key and value into the StringDictionary myDict.Add("A", "Apple"); myDict.Add("B", "Banana"); myDict.Add("C", "Cat"); myDict.Add("D", "Dog"); myDict.Add("E", "Elephant"); myDict.Add("F", "Fish"); // Displaying the keys and values in StringDictionary Console.WriteLine("The number of key/value pairs are : " + myDict.Count); foreach(DictionaryEntry dic in myDict) { Console.WriteLine(dic.Key + " " + dic.Value); } // Removing the entry with the specified // key from the StringDictionary myDict.Remove("D"); // Displaying the keys and values in StringDictionary Console.WriteLine("The number of key/value pairs are : " + myDict.Count); foreach(DictionaryEntry dic in myDict) { Console.WriteLine(dic.Key + " " + dic.Value); } } } Output: The number of key/value pairs are : 6 b Banana c Cat a Apple f Fish d Dog e Elephant The number of key/value pairs are : 5 b Banana c Cat a Apple f Fish e Elephant Example 2: CSHARP // C# code to remove the entry // with the specified key from // the StringDictionary using System; using System.Collections; using System.Collections.Specialized; class GFG { // Driver code public static void Main() { // Creating a StringDictionary named myDict StringDictionary myDict = new StringDictionary(); // Adding key and value into the StringDictionary myDict.Add("A", "Apple"); myDict.Add("B", "Banana"); myDict.Add("C", "Cat"); myDict.Add("D", "Dog"); myDict.Add("E", "Elephant"); myDict.Add("F", "Fish"); // Displaying the keys and values in StringDictionary Console.WriteLine("The number of key/value pairs are : " + myDict.Count); foreach(DictionaryEntry dic in myDict) { Console.WriteLine(dic.Key + " " + dic.Value); } // Removing the entry with the specified // key from the StringDictionary // This should raise "ArgumentNullException" // as the key is null myDict.Remove(null); // Displaying the keys and values in StringDictionary Console.WriteLine("The number of key/value pairs are : " + myDict.Count); foreach(DictionaryEntry dic in myDict) { Console.WriteLine(dic.Key + " " + dic.Value); } } } Runtime Error: Unhandled Exception: System.ArgumentNullException: Value cannot be null. Parameter name: key Note: If the StringDictionary does not contain an element with the specified key, the StringDictionary remains unchanged. No exception is thrown. The key is handled in a case-insensitive manner, i.e, it is translated to lowercase before it is used to find the entry to remove from the StringDictionary. This method is an O(1) operation. Reference: https://learn.microsoft.com/en-us/dotnet/api/system.collections.specialized.stringdictionary.remove?view=netframework-4.7.2 Comment More infoAdvertise with us Next Article C# | Remove from the specified index of the StringCollection S Sahil_Bansall Follow Improve Article Tags : C# CSharp-method CSharp-Collections-Namespace CSharp-Specialized-StringDictionary CSharp-Specialized-Namespace +1 More Similar Reads C# | Remove from the specified index of the StringCollection StringCollection class is a new addition to the .NET Framework class library that represents a collection of strings. StringCollection class is defined in the System.Collections.Specialized namespace. StringCollection.RemoveAt(Int32) method is used to remove the string at the specified index of the 3 min read C# | Remove from the specified index of the StringCollection StringCollection class is a new addition to the .NET Framework class library that represents a collection of strings. StringCollection class is defined in the System.Collections.Specialized namespace. StringCollection.RemoveAt(Int32) method is used to remove the string at the specified index of the 3 min read C# | Gets or sets the value at the specified key in StringDictionary StringDictionary.Item[String] Property is used to get or set the value associated with the specified key. Syntax: public virtual string this[string key] { get; set; } Here, key is the Key of type System.String whose value is be to get or set. Return Value: This property returns the value associated 3 min read C# | Gets or sets the value at the specified key in StringDictionary StringDictionary.Item[String] Property is used to get or set the value associated with the specified key. Syntax: public virtual string this[string key] { get; set; } Here, key is the Key of type System.String whose value is be to get or set. Return Value: This property returns the value associated 3 min read C# | Removing all entries from the StringDictionary StringDictionary.Clear method is used to remove all the entries from the StringDictionary. Syntax: public virtual void Clear (); Exception: This method will give the NotSupportedException if the StringDictionary is read-only. Example: CSHARP // C# code to remove all entries // from the StringDiction 2 min read C# | Removing all entries from the StringDictionary StringDictionary.Clear method is used to remove all the entries from the StringDictionary. Syntax: public virtual void Clear (); Exception: This method will give the NotSupportedException if the StringDictionary is read-only. Example: CSHARP // C# code to remove all entries // from the StringDiction 2 min read Like