C# | Remove entry with specified key from OrderedDictionary (original) (raw)
Last Updated : 01 Feb, 2019
OrderedDictionary.Remove(Object) method is used to remove entry with the specified key from the OrderedDictionary collection.Syntax:
public void Remove (object key);
Here, key is the key of the entry to remove.Exceptions:
- NotSupportedException : If the OrderedDictionary collection is read-only.
- ArgumentNullException : If the key is null.
Below given are some examples to understand the implementation in a better way: Example 1:
CSHARP `
// C# code to remove the entry // with the specified key from // the OrderedDictionary using System; using System.Collections; using System.Collections.Specialized;
class GFG {
// Driver method
public static void Main()
{
// Creating a orderedDictionary named myDict
OrderedDictionary myDict = new OrderedDictionary();
// Adding key and value in myDict
myDict.Add("key1", "value1");
myDict.Add("key2", "value2");
myDict.Add("key3", "value3");
myDict.Add("key4", "value4");
myDict.Add("key5", "value5");
// Displaying the number of element initially
Console.WriteLine("Number of elements are : "
+ myDict.Count);
// Displaying the elements in myDict
foreach(DictionaryEntry de in myDict)
Console.WriteLine(de.Key + " -- " + de.Value);
// Removing the entry with the specified
// key from the OrderedDictionary
myDict.Remove("key2");
// Displaying the number of element initially
Console.WriteLine("Number of elements are : "
+ myDict.Count);
// Displaying the elements in myDict
foreach(DictionaryEntry de in myDict)
Console.WriteLine(de.Key + " -- " + de.Value);
}
}
`
Output:
Number of elements are : 5 key1 -- value1 key2 -- value2 key3 -- value3 key4 -- value4 key5 -- value5 Number of elements are : 4 key1 -- value1 key3 -- value3 key4 -- value4 key5 -- value5
Example 2:
CSHARP `
// C# code to remove the entry // with the specified key from // the OrderedDictionary using System; using System.Collections; using System.Collections.Specialized;
class GFG {
// Driver method
public static void Main()
{
// Creating a orderedDictionary named myDict
OrderedDictionary myDict = new OrderedDictionary();
// Adding key and value in myDict
myDict.Add("A", "Apple");
myDict.Add("B", "Banana");
myDict.Add("C", "Cat");
myDict.Add("D", "Dog");
// Displaying the number of element initially
Console.WriteLine("Number of elements are : "
+ myDict.Count);
// Displaying the elements in myDict
foreach(DictionaryEntry de in myDict)
Console.WriteLine(de.Key + " -- " + de.Value);
// Removing the entry with the specified
// key from the OrderedDictionary
// This should raise "ArgumentNullException"
// as the key is null
myDict.Remove(null);
// Displaying the number of element initially
Console.WriteLine("Number of elements are : "
+ myDict.Count);
// Displaying the elements in myDict
foreach(DictionaryEntry de in myDict)
Console.WriteLine(de.Key + " -- " + de.Value);
}
}
`
Runtime Error:
Unhandled Exception: System.ArgumentNullException: Value cannot be null. Parameter name: key
Note:
- The entries that follow the removed entry move up to occupy the vacated spot and the indexes of the entries that get moved are also updated.
- If the OrderedDictionary collection does not contain an entry with the specified key, the OrderedDictionary remains unchanged and no exception is thrown.
Reference:
Similar Reads
- C# | Remove the entry at specified index from OrderedDictionary OrderedDictionary.RemoveAt(Int32) method is used to remove the entry at the specified index from the OrderedDictionary collection. Syntax: public void RemoveAt (int index); Here, index is the zero-based index of the entry to remove. Exceptions: NotSupportedException : If the OrderedDictionary collec 3 min read
- C# | Remove the entry with specified key from ListDictionary ListDictionary.Remove(Object) method is used to remove the entry with the specified key from the ListDictionary. Syntax: public void Remove (object key); Here, key is the key of the entry which is to be removed. Exception: This method will give ArgumentNullException if the key is null. Below are the 3 min read
- C# | Removing the specified key entry from HybridDictionary HybridDictionary.Remove(Object) method is used to remove the entry with the specified key from the HybridDictionary. Syntax: public void Remove (object key); Here, key is the key of the entry to remove. Exception: This method throws ArgumentNullException if the key is null. Below given are some exam 3 min read
- C# | Insert a new entry in OrderedDictionary with specified key and value OrderedDictionary.Insert(Int32, Object, Object) Method is used to inserts a new entry into the OrderedDictionary collection with the specified key and value at the specified index. Syntax: public void Insert (int index, object key, object value); Parameters: index: It is the zero-based index of type 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
- C# | Insert into OrderedDictionary with key and value at specified index OrderedDictionary.Insert(Int32, Object, Object) method is used to insert a new entry into the OrderedDictionary collection with the specified key and value at the specified index. Syntax: public void Insert (int index, object key, object value); Parameters: index : It is the zero-based index at whic 3 min read
- C# | Check if SortedDictionary contains the specified key or not SortedDictionary<TKey, TValue>.ContainsKey(TKey) Method is used to check whether the SortedDictionary contains an element with the specified key or not. Syntax: public bool ContainsKey (TKey key); Here, the key is the Key which is to be located in the SortedDictionary. Returns Value: This meth 2 min read
- C# | Remove entry with specified key from the StringDictionary 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 Str 3 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# | Removing all entries from HybridDictionary HybridDictionary.Clear method is used to remove all entries from the HybridDictionary. Syntax: public void Clear (); Below given are some examples to understand the implementation in a better way : Example 1: CSHARP // C# code to removes all entries // from the HybridDictionary. using System; using 3 min read