C# | Dictionary.Clear Method Last Updated : 01 Feb, 2019 Summarize Comments Improve Suggest changes Share Like Article Like Report This method is used to remove all key/value pairs from the Dictionary<TKey,TValue>. Syntax: public void Clear (); Below are the programs to illustrate the use of above-discussed method: Example 1: csharp // C# code to remove all pairs // from Dictionary using System; using System.Collections.Generic; class GFG { // Driver code public static void Main() { // Create a new dictionary of // strings, with string keys. Dictionary<string, string> myDict = new Dictionary<string, string>(); // Adding key/value pairs in myDict myDict.Add("Australia", "Canberra"); myDict.Add("Belgium", "Brussels"); myDict.Add("Netherlands", "Amsterdam"); myDict.Add("China", "Beijing"); myDict.Add("Russia", "Moscow"); myDict.Add("India", "New Delhi"); // To get count of key/value pairs in myDict Console.WriteLine("Total key/value pairs "+ "in myDict are : " + myDict.Count); // Remove all pairs from the Dictionary myDict.Clear(); Console.WriteLine("After clear operation"); // To get count of key/value pairs in myDict Console.WriteLine("Total key/value pairs in"+ " myDict are : " + myDict.Count); } } Output: Total key/value pairs in myDict are : 6 After clear operation Total key/value pairs in myDict are : 0 Example 2: csharp // C# code to remove all pairs // from Dictionary using System; using System.Collections.Generic; class GFG { // Driver code public static void Main() { // Create a new dictionary of // strings, with string keys. Dictionary<int, int> myDict = new Dictionary<int, int>(); // Adding key/value pairs in myDict myDict.Add(9, 8); myDict.Add(3, 4); myDict.Add(4, 7); myDict.Add(1, 7); // To get count of key/value pairs in myDict Console.WriteLine("Total key/value pairs "+ "in myDict are : " + myDict.Count); // Remove all pairs from the Dictionary myDict.Clear(); Console.WriteLine("After clear operation"); // To get count of key/value pairs in myDict Console.WriteLine("Total key/value pairs"+ " in myDict are : " + myDict.Count); } } Output: Total key/value pairs in myDict are : 4 After clear operation Total key/value pairs in myDict are : 0 Reference: https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2.clear?view=netframework-4.7.2 Comment More infoAdvertise with us Next Article C# | Dictionary.Clear Method R rupesh_rao Follow Improve Article Tags : C# CSharp-method CSharp-Generic-Namespace CSharp Dictionary Class Similar Reads C# | Dictionary.Remove Method This method is used to remove the value with the specified key from the Dictionary<TKey,TValue>. Syntax: public bool Remove (TKey key); Return Value: This method returns true if the element is successfully found and removed; otherwise it returns false. This method returns false if key is not f 2 min read C# Dictionary Class In C#, the Dictionary class is the part of the System.Collections.Generic namespace. It is a Collection that stores Key-value pairs. Each key in the dictionary is unique and each key maps to a single value.In Dictionary, each entry consists of a key and its associated value.It provides constant time 7 min read Console.Clear Method in C# This method is used to clear the console buffer and corresponding console window of display information. Syntax: public static void Clear (); Exceptions: This method throws IOException if an I/O error occurred. Below programs show the use of Console.Clear() method: Program 1: To display the contents 1 min read Stack.Clear Method in C# This method(comes under System.Collections namespace) is used to remove all the objects from the Stack. This method will set the Count of Stack to zero, and references to other objects from elements of the collection are also removed. This method is an O(n) operation, where n is Count. Syntax: publi 2 min read Queue.Clear Method in C# This method is used to remove the objects from the Queue. This method is an O(n) operation, where n is the total count of elements. And this method comes under System.Collections namespace. Syntax: public void Clear (); Below given are some examples to understand the implementation in a better way: 2 min read Like