using System.Collections; using System.Collections.Generic; Dictionarydict = new Dictionary (); dict.Add("apple", 1); dict.Add("banana", 2); dict.Add("orange", 3); Console.WriteLine("Dictionary before clear:"); foreach (KeyValuePair kvp in dict) { Console.WriteLine($"Key: {kvp.Key}, Value: {kvp.Value}"); } dict.Clear(); Console.WriteLine("Dictionary after clear:"); foreach (KeyValuePair kvp in dict) { Console.WriteLine($"Key: {kvp.Key}, Value: {kvp.Value}"); }
Dictionary before clear: Key: apple, Value: 1 Key: banana, Value: 2 Key: orange, Value: 3 Dictionary after clear:
using System.Collections; Hashtable hashtable = new Hashtable(); hashtable.Add("one", 1); hashtable.Add("two", 2); hashtable.Add("three", 3); hashtable.Clear();In this example, we create a hashtable (another data structure that stores key-value pairs) with three key-value pairs, and then call the Clear method to remove all the elements from it. Package library: This feature is present in the .NET Framework Class Library.