Sorteddictionary.Clear() Method In C#

In this article, we will discuss about SortedDictionary.Clear method in C#. A SortedDictionary is a general-purpose collection used to store key-value pairs in a sorted format, where the sorting is based on the key SortedDictionary is defined in the System.Collection.Generic namespace. This is dynamic, which means that the size of the sorted dictionary grows as needed.

What is SortedDictionary?

SortedDictionary requires keys to be unique. The use of duplicate keys is prohibited. In SortedDictionary, the keys are immutable and cannot be null. SortedDictionary allows values to be null if the value type is a reference type. It provides the fastest insert and delete operations on unsorted data. A SortedDictionary can only store key-value pairs of the same type. The capacity of a SortedDictionary is the number of key-value pairs that the SortedDictionary can hold.

The SortedDictionary.Clear method removes all key-value pairs from the SortedDictionary.

Syntax:

It has the following syntax:

Example

public void clear();

The below program will be used to describe the SortedDictionary.Clear method :

Example 1:

Let us take an example to implement the SortedDictionary.clear method in C#

Example

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:

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 us take another example to implement the SortedDictionary.clear method in C#

Example

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:

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

Input Required

This code uses input(). Please provide values below: