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
Dictionary.Clear Method in C#
The Dictionary.Clear() method in C# removes all key/value pairs from the Dictionary<TKey,TValue>. This method is useful when you need to empty a dictionary completely while keeping the dictionary object itself for future use.
Syntax
public void Clear();
Parameters
The Clear() method takes no parameters.
Return Value
The Clear() method does not return any value. It has a void return type.
Using Dictionary.Clear() Method
Example
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
Dictionary<string, string> dict = new Dictionary<string, string>();
dict.Add("One", "John");
dict.Add("Two", "Tom");
dict.Add("Three", "Jacob");
dict.Add("Four", "Kevin");
dict.Add("Five", "Nathan");
Console.WriteLine("Count of elements = " + dict.Count);
Console.WriteLine("\nKey/value pairs...");
foreach(KeyValuePair<string, string> res in dict) {
Console.WriteLine("Key = {0}, Value = {1}", res.Key, res.Value);
}
dict.Clear();
Console.WriteLine("Cleared Key/value pairs...");
foreach(KeyValuePair<string, string> res in dict) {
Console.WriteLine("Key = {0}, Value = {1}", res.Key, res.Value);
}
Console.WriteLine("Count of elements now = " + dict.Count);
}
}
The output of the above code is −
Count of elements = 5 Key/value pairs... Key = One, Value = John Key = Two, Value = Tom Key = Three, Value = Jacob Key = Four, Value = Kevin Key = Five, Value = Nathan Cleared Key/value pairs... Count of elements now = 0
Clear vs. Reassignment
Example
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
Dictionary<int, string> numbers = new Dictionary<int, string>();
numbers.Add(1, "One");
numbers.Add(2, "Two");
numbers.Add(3, "Three");
Console.WriteLine("Original Count: " + numbers.Count);
// Using Clear() method
numbers.Clear();
Console.WriteLine("After Clear(): " + numbers.Count);
// Dictionary can be reused after Clear()
numbers.Add(10, "Ten");
numbers.Add(20, "Twenty");
Console.WriteLine("After adding new elements: " + numbers.Count);
foreach(var pair in numbers) {
Console.WriteLine("Key = {0}, Value = {1}", pair.Key, pair.Value);
}
}
}
The output of the above code is −
Original Count: 3 After Clear(): 0 After adding new elements: 2 Key = 10, Value = Ten Key = 20, Value = Twenty
Common Use Cases
-
Resetting a cache − Clear all cached data before repopulating.
-
Memory management − Remove all references to help garbage collection.
-
Reusing dictionaries − Empty a dictionary for reuse in loops or different contexts.
-
State management − Reset application state stored in dictionaries.
Conclusion
The Dictionary.Clear() method efficiently removes all key-value pairs from a dictionary, setting its count to zero while keeping the dictionary object intact for future use. This method is essential for scenarios requiring complete dictionary reset without creating new dictionary instances.
