In this tutorial, we will explore the SortedDictionary.Clear method in C#. A SortedDictionary serves as a versatile container for organizing key-value pairs in a sorted manner, sorted according to the keys. SortedDictionary is specified within the System.Collection.Generic namespace. It is a dynamic structure, allowing the sorted dictionary to expand as required.
What is SortedDictionary?
SortedDictionary mandates keys to be distinct, making the inclusion of duplicate keys impermissible. In SortedDictionary, keys remain unchangeable and cannot be null. While SortedDictionary permits null values for reference types, it excels in rapid insert and delete operations on unsorted data. Additionally, a SortedDictionary exclusively accommodates key-value pairs of identical types, with its capacity denoting the maximum number of pairs it can contain.
The SortedDictionary.Clear function eliminates all key-value pairs from the SortedDictionary data structure.
Syntax:
It has the following syntax:
public void clear();
The following code snippet demonstrates the application of the SortedDictionary.Clear method:
Example 1:
Let's consider an example to demonstrate the utilization of the SortedDictionary.clear method in C#.
using System;
using System.Collections.Generic;
class SortedDictionaryClear{
// Driver code
public static void Main()
{
//The new dictionary is created
//with the string values
SortedDictionary<string, string> dict =
new SortedDictionary<string, string>();
// the key & value pairs are
//added to the dictionary
dict.Add("Afghanistan", "Kabul");
dict.Add("Sri Lanka", "Colombo");
dict.Add("Switzerland", "Berne");
dict.Add("Thailand","Bangkok");
dict.Add("Tunisia","Tunis");
dict.Add("Ukraine","Kiev");
dict.Add("Sweden","Stockholm");
dict.Add("Tajikistan","Dushanbe");
// finding the total key&value pairs in dict
Console.WriteLine("The total number of key/value pairs "
+ "in dict is: " + dict.Count);
// clear() method to remove all key&value pairs in dict
dict.Clear();
Console.WriteLine("The total pairs after the clear() method:");
// finding the total key&value pairs in dict
Console.WriteLine("The total number of key/value pairs in"
+ " dict is: " + dict.Count);
}
}
Output:
The total number of key/value pairs in dict is: 8
The total pairs after the clear() method:
The total number of key/value pairs in dict is: 0
Example 2:
Let's consider another instance to apply the SortedDictionary.clear function in C#.
using System;
using System.Collections.Generic;
class SortedDictionaryClear{
// Driver code
public static void Main()
{
//The new dictionary is created
//with the Integer values
SortedDictionary<int, int> dict =
new SortedDictionary<int, int>();
// the key & value pairs are
//added to the dictionary
dict.Add(1,11);
dict.Add(2,22);
dict.Add(3,33);
dict.Add(4,44);
dict.Add(5,55);
dict.Add(6,66);
dict.Add(7,77);
dict.Add(8,88);
dict.Add(9,99);
// finding the total key&value pairs in dict
Console.WriteLine("The total number of key/value pairs "
+ "in dict is: " + dict.Count);
// clear() method to remove all key&value pairs in dict
dict.Clear();
Console.WriteLine("The total pairs after the clear() method:");
// finding the total key&value pairs in dict
Console.WriteLine("The total number of key/value pairs in"
+ " dict is: " + dict.Count);
}
}
Output:
The total number of key/value pairs in dict is: 9
The total pairs after the clear() method:
The total number of key/value pairs in dict is : 0