Open In App

C# | Dictionary.Clear Method

Last Updated : 01 Feb, 2019
Improve
Improve
Like Article
Like
Save
Share
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:




// 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:




// 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:



Similar Reads

C# | Array.Clear() Method
This method is used to set a range of elements in an array to the default value of each element type. Syntax: public static void Clear (Array array, int index, int length); Parameters: array: It is an array whose elements need to be cleared. index: It is the starting index of the range of elements to clear. length: It is the number of elements to c
3 min read
C# | SortedDictionary.Clear() Method
This method is used to remove all key/value pairs from the SortedDictionary&lt;TKey, TValue&gt;. Syntax: public void Clear (); Below are the programs to illustrate the use of the above-discussed method: Example 1: // C# code to remove all pairs // from SortedDictionary using System; using System.Collections.Generic; class GFG { // Driver code 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: Example 1: // C# code to illustrate the // Queue.C
2 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: public virtual void Clear (); Below programs illustrate
2 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 before the use of Clear method // C# program to i
1 min read
Graphics.Clear() Method in C# with Examples
Graphics.Clear(color) Method is used to clear the canvas and paints it up with the specified background color. Syntax: public void Clear (System.Drawing.Color color); Parameter: color: Color identifier which contains RGB values, to colour the background of canvas. Example 1: // C# snippet to print GeeksForGeeks using System; using System.Drawing; u
1 min read
C# | Dictionary.Add() Method
Dictionary&lt;TKey,TValue&gt;.Add() Method is used to add a specified key and value to the dictionary. Syntax: public void Add (TKey key, TValue value); Parameters: key: It is the key of the element to add. value: It is the value of the element to add. The value can be null for reference types. Exceptions: ArgumentNullException : If the key is null
3 min read
C# | Dictionary.ContainsValue() Method
This method is used to check whether the Dictionary&lt;TKey,TValue&gt; contains a specific value or not. Syntax: public bool ContainsValue (TValue value); Here, the value is the Value to locate in the Dictionary. The value can be null for reference types. Return Value: This method returns true if the Dictionary contains an element with the specifie
2 min read
C# | Dictionary.ContainsKey() Method
This method is used to check whether the Dictionary&lt;TKey,TValue&gt; contains the specified key or not. Syntax: public bool ContainsKey (TKey key); Here, the key is the Key which is to be located in the Dictionary. Return Value: This method will return true if the Dictionary contains an element with the specified key otherwise, it returns false.
2 min read
C# | Dictionary.Remove Method
This method is used to remove the value with the specified key from the Dictionary&lt;TKey,TValue&gt;. 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 found in the Dictionary. Exception: This method wil
2 min read