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
SortedDictionary.Clear() Method in C#
The SortedDictionary.Clear() method in C# is used to remove all key-value pairs from the SortedDictionary<TKey, TValue>. This method provides an efficient way to empty the entire collection in a single operation.
Syntax
Following is the syntax for the Clear() −
public void Clear();
Parameters
This method takes no parameters.
Return Value
This method does not return any value (void).
Using Clear() Method with Electronic Items
The following example demonstrates how Clear() removes all items from the SortedDictionary −
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
SortedDictionary<int, string> sortedDict = new SortedDictionary<int, string>();
sortedDict.Add(100, "Mobile");
sortedDict.Add(200, "Laptop");
sortedDict.Add(300, "Desktop");
sortedDict.Add(400, "Speakers");
sortedDict.Add(500, "Headphone");
sortedDict.Add(600, "Earphone");
Console.WriteLine("SortedDictionary key-value pairs...");
foreach (var pair in sortedDict) {
Console.WriteLine("Key = " + pair.Key + ", Value = " + pair.Value);
}
Console.WriteLine("Count before Clear(): " + sortedDict.Count);
Console.WriteLine("Contains key 200? " + sortedDict.ContainsKey(200));
sortedDict.Clear();
Console.WriteLine("Count after Clear(): " + sortedDict.Count);
}
}
The output of the above code is −
SortedDictionary key-value pairs... Key = 100, Value = Mobile Key = 200, Value = Laptop Key = 300, Value = Desktop Key = 400, Value = Speakers Key = 500, Value = Headphone Key = 600, Value = Earphone Count before Clear(): 6 Contains key 200? True Count after Clear(): 0
Using Clear() After Adding More Items
This example shows how Clear() works after dynamically adding more items to the SortedDictionary −
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
SortedDictionary<int, string> sortedDict = new SortedDictionary<int, string>();
sortedDict.Add(100, "Inspiron");
sortedDict.Add(200, "Alienware");
sortedDict.Add(300, "Projectors");
sortedDict.Add(400, "XPS");
Console.WriteLine("Initial SortedDictionary:");
foreach (var pair in sortedDict) {
Console.WriteLine("Key = " + pair.Key + ", Value = " + pair.Value);
}
Console.WriteLine("Count: " + sortedDict.Count);
sortedDict.Add(800, "Notebook");
sortedDict.Add(10000, "Bluetooth Speaker");
Console.WriteLine("\nAfter adding more items:");
foreach (var pair in sortedDict) {
Console.WriteLine("Key = " + pair.Key + ", Value = " + pair.Value);
}
Console.WriteLine("Updated count: " + sortedDict.Count);
sortedDict.Clear();
Console.WriteLine("\nCount after Clear(): " + sortedDict.Count);
}
}
The output of the above code is −
Initial SortedDictionary: Key = 100, Value = Inspiron Key = 200, Value = Alienware Key = 300, Value = Projectors Key = 400, Value = XPS Count: 4 After adding more items: Key = 100, Value = Inspiron Key = 200, Value = Alienware Key = 300, Value = Projectors Key = 400, Value = XPS Key = 800, Value = Notebook Key = 10000, Value = Bluetooth Speaker Updated count: 6 Count after Clear(): 0
Key Benefits of Clear() Method
-
Efficiency: Removes all elements in a single operation instead of removing items one by one.
-
Memory Management: Helps free up memory by removing all references to stored objects.
-
Reset State: Returns the SortedDictionary to its initial empty state, ready for reuse.
Conclusion
The SortedDictionary.Clear() method efficiently removes all key-value pairs from the collection, setting its count to zero. This method is useful for resetting the dictionary state and freeing memory when all stored data is no longer needed.
