Dictionarydictionary = new Dictionary (); dictionary.Add("apple", 1); dictionary.Add("banana", 2); if (dictionary.ContainsKey("apple")) { Console.WriteLine("The dictionary contains the key 'apple'"); } else { Console.WriteLine("The dictionary does not contain the key 'apple'"); }
DictionaryIn this example, we create a dictionary with two key-value pairs, and then check if the dictionary contains the key 3. Since the dictionary does not have a key with the value of 3, we print a message saying so. The System.ContainsKey method is part of the System.Collections.Generic package.dictionary = new Dictionary (); dictionary.Add(1, "John"); dictionary.Add(2, "Mary"); if (dictionary.ContainsKey(3)) { Console.WriteLine("The dictionary contains the key 3"); } else { Console.WriteLine("The dictionary does not contain the key 3"); }