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 all elements from OrderedDictionary in C#
The OrderedDictionary class in C# provides the Clear() method to remove all elements from the collection. This method is efficient and maintains the structure of the OrderedDictionary while removing all key-value pairs.
The OrderedDictionary combines features of both Hashtable and ArrayList, allowing access to elements by both key and index while preserving insertion order.
Syntax
Following is the syntax for the Clear() method −
public void Clear()
Parameters
The Clear() method takes no parameters.
Return Value
The Clear() method does not return any value. It simply removes all elements from the OrderedDictionary.
Example
Following example demonstrates how to clear all elements from an OrderedDictionary −
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main(){
OrderedDictionary dict = new OrderedDictionary();
dict.Add("A", "Books");
dict.Add("B", "Electronics");
dict.Add("C", "Smart Wearables");
dict.Add("D", "Pet Supplies");
dict.Add("E", "Clothing");
dict.Add("F", "Footwear");
Console.WriteLine("OrderedDictionary elements...");
foreach(DictionaryEntry d in dict){
Console.WriteLine(d.Key + " " + d.Value);
}
Console.WriteLine("Count of elements in OrderedDictionary = " + dict.Count);
dict.Clear();
Console.WriteLine("Count of elements in OrderedDictionary (Updated)= " + dict.Count);
}
}
The output of the above code is −
OrderedDictionary elements... A Books B Electronics C Smart Wearables D Pet Supplies E Clothing F Footwear Count of elements in OrderedDictionary = 6 Count of elements in OrderedDictionary (Updated)= 0
Using Clear() with Numeric Keys
The following example shows clearing an OrderedDictionary with numeric string keys −
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main(){
OrderedDictionary dict = new OrderedDictionary();
dict.Add("1", "AB");
dict.Add("2", "CD");
dict.Add("3", "EF");
dict.Add("4", "GH");
Console.WriteLine("OrderedDictionary elements...");
foreach(DictionaryEntry d in dict){
Console.WriteLine(d.Key + " " + d.Value);
}
Console.WriteLine("Count of elements in OrderedDictionary = " + dict.Count);
dict.Clear();
Console.WriteLine("Count of elements in OrderedDictionary (Updated)= " + dict.Count);
// Adding elements after clearing
dict.Add("X", "New Value");
Console.WriteLine("After adding new element, Count = " + dict.Count);
}
}
The output of the above code is −
OrderedDictionary elements... 1 AB 2 CD 3 EF 4 GH Count of elements in OrderedDictionary = 4 Count of elements in OrderedDictionary (Updated)= 0 After adding new element, Count = 1
How It Works
The Removes all key-value pairs from the OrderedDictionary Sets the Maintains the internal structure for future additions Does not affect the capacity of the OrderedDictionary The Clear()
Count property to zeroConclusion
Clear() method provides an efficient way to remove all elements from an OrderedDictionary in C#. After clearing, the OrderedDictionary remains usable and you can add new elements to it, maintaining the insertion order for subsequent additions.
