In this tutorial, I’ll show you how to remove the given key from a dictionary.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | class Program { static void Main(string[] args) { int mul = 1; // New Dictionary Dictionary<string, int> dic = new Dictionary<string, int>(); dic.Add("num1", 10); dic.Add("num2", 32); dic.Add("num3", 11); dic.Add("num4", 9); dic.Add("num5", 10); Console.WriteLine("List of Dictionary"); Console.WriteLine("<::::::::::::::>"); foreach (var x in dic) { Console.WriteLine("Key: {0}, Value: {1}", x.Key, x.Value); } Console.Write("\n\nEnter a key value which do you want to delete:"); string key = Console.ReadLine(); dic.Remove(key); Console.WriteLine("New: List of Dictionary"); Console.WriteLine("<::::::::::::::>"); foreach (var x in dic) { Console.WriteLine("Key: {0}, Value: {1}", x.Key, x.Value); } Console.ReadLine(); } } |
Output: