Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Remove entry with specified key from the StringDictionary in C#
The StringDictionary class in C# provides the Remove() method to delete entries with a specified key. This method removes both the key and its associated value from the collection. StringDictionary is case-insensitive, meaning keys are automatically converted to lowercase.
Syntax
Following is the syntax for the Remove() method −
public virtual void Remove(string key)
Parameters
- key − The string key to remove from the StringDictionary.
Using Remove() Method
Example
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
StringDictionary strDict1 = new StringDictionary();
strDict1.Add("A", "John");
strDict1.Add("B", "Andy");
strDict1.Add("C", "Tim");
strDict1.Add("D", "Ryan");
strDict1.Add("E", "Kevin");
strDict1.Add("F", "Katie");
strDict1.Add("G", "Brad");
Console.WriteLine("StringDictionary1 key-value pairs...");
foreach(DictionaryEntry de in strDict1) {
Console.WriteLine(de.Key + " " + de.Value);
}
strDict1.Remove("D");
Console.WriteLine("\nStringDictionary1 key-value pairs after removing a key...");
foreach(DictionaryEntry de in strDict1) {
Console.WriteLine(de.Key + " " + de.Value);
}
StringDictionary strDict2 = new StringDictionary();
strDict2.Add("1", "A");
strDict2.Add("2", "B");
strDict2.Add("3", "C");
strDict2.Add("4", "D");
strDict2.Add("5", "E");
Console.WriteLine("\nStringDictionary2 key-value pairs...");
IEnumerator demoEnum = strDict2.GetEnumerator();
DictionaryEntry d;
while (demoEnum.MoveNext()) {
d = (DictionaryEntry)demoEnum.Current;
Console.WriteLine("Key = " + d.Key + ", Value = " + d.Value);
}
}
}
The output of the above code is −
StringDictionary1 key-value pairs... a John b Andy c Tim d Ryan e Kevin f Katie g Brad StringDictionary1 key-value pairs after removing a key... a John b Andy c Tim e Kevin f Katie g Brad StringDictionary2 key-value pairs... Key = 1, Value = A Key = 2, Value = B Key = 3, Value = C Key = 4, Value = D Key = 5, Value = E
Removing Multiple Keys
Example
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
StringDictionary strDict1 = new StringDictionary();
strDict1.Add("A", "One");
strDict1.Add("B", "Two");
strDict1.Add("C", "Three");
strDict1.Add("D", "Four");
strDict1.Add("E", "Five");
Console.WriteLine("StringDictionary1 key-value pairs...");
foreach(DictionaryEntry de in strDict1) {
Console.WriteLine(de.Key + " " + de.Value);
}
strDict1.Remove("B");
strDict1.Remove("C");
Console.WriteLine("\nStringDictionary1 key-value pairs after removing keys...");
foreach(DictionaryEntry de in strDict1) {
Console.WriteLine(de.Key + " " + de.Value);
}
}
}
The output of the above code is −
StringDictionary1 key-value pairs... a One b Two c Three d Four e Five StringDictionary1 key-value pairs after removing keys... a One d Four e Five
Key Points
The
Remove()method is case-insensitive ? keys are converted to lowercase internally.If the specified key does not exist, the method does nothing and no exception is thrown.
After removal, the
Countproperty is decremented by one.Multiple keys can be removed by calling
Remove()multiple times.
Conclusion
The Remove() method in StringDictionary provides an efficient way to delete key-value pairs by specifying the key. Remember that StringDictionary operations are case-insensitive, and removing non-existent keys does not raise exceptions.
