The System.Collections.Generic.Dictionary is a collection, provided in the System.Collections.Generic package library, that uses key-value pairs to store and retrieve data. The ContainsKey method is used to determine whether a dictionary contains a specific key.
if (myDictionary.ContainsKey(2)) { Console.WriteLine("The dictionary contains the key 2."); } else { Console.WriteLine("The dictionary does not contain the key 2."); }
Output: The dictionary contains the key 2.
In this example, a dictionary is created and three key-value pairs are added to it. The ContainsKey method is then used to check if the key 2 exists in the dictionary. Since it does exist, the output is "The dictionary contains the key 2."
if (myDictionary.ContainsKey(searchKey)) { Console.WriteLine("The dictionary contains the key " + searchKey + "."); } else { Console.WriteLine("The dictionary does not contain the key " + searchKey + "."); }
Output: The dictionary does not contain the key orange.
In this example, a dictionary is initialized with three key-value pairs using collection initializer syntax. The ContainsKey method is then used to check if the key "orange" exists in the dictionary. Since it does not exist, the output is "The dictionary does not contain the key orange."
C# (CSharp) System.Collections.Generic System.Collections.Generic.Dictionary.ContainsKey - 48 examples found. These are the top rated real world C# (CSharp) examples of System.Collections.Generic.System.Collections.Generic.Dictionary.ContainsKey extracted from open source projects. You can rate examples to help us improve the quality of examples.