DictionarymyDictionary = new Dictionary (); myDictionary.Add("key1", "value1"); myDictionary.Add("key2", "value2"); myDictionary.Clear(); Console.WriteLine("Count after Clear(): " + myDictionary.Count); // Output: Count after Clear(): 0
using System.Collections.Generic; using System.Windows.Forms; namespace MyNamespace { public partial class MainForm : Form { private DictionaryIn this example, we have a WinForms application with a button that clears a dictionary when clicked. We declare the dictionary as a private field of the MainForm class and initialize it in the constructor. When the user clicks the button, the btnClear_Click method is called, which in turn calls the Clear() method on the dictionary. Package/Library: This functionality is available in the .NET class library, which is included in the .NET Framework SDK. The namespace for this specific feature is System.Collections.Generic.myDictionary; public MainForm() { InitializeComponent(); myDictionary = new Dictionary (); } private void btnClear_Click(object sender, EventArgs e) { myDictionary.Clear(); } } }