StringDictionary.ContainsKey(String) method is used to check whether the StringDictionary contains a specific key or not.
Syntax:
public virtual bool ContainsKey (string key);
Here, key is the key to locate in the StringDictionary.
Return Value: This method returns true if the StringDictionary contains an entry with the specified key, otherwise it returns the false.
Exception: This method will give ArgumentNullException if the key is null.
Below programs illustrate the use of StringDictionary.ContainsKey(String) method:
Example 1:
// C# code to check if the // StringDictionary contains // a specific key using System; using System.Collections; using System.Collections.Specialized; class GFG { // Driver code public static void Main() { // Creating a StringDictionary named myDict StringDictionary myDict = new StringDictionary(); // Adding key and value into the StringDictionary myDict.Add("G", "Geeks"); myDict.Add("F", "For"); myDict.Add("C", "C++"); myDict.Add("DS", "Data Structures"); myDict.Add("N", "Noida"); // Checking if "DS" is contained in // StringDictionary myDict if (myDict.ContainsKey("DS")) Console.WriteLine("StringDictionary myDict contains the key"); else Console.WriteLine("StringDictionary myDict does not contain the key"); } } |
Output:
StringDictionary myDict contains the key
Example 2:
// C# code to check if the // StringDictionary contains // a specific key using System; using System.Collections; using System.Collections.Specialized; class GFG { // Driver code public static void Main() { // Creating a StringDictionary named myDict StringDictionary myDict = new StringDictionary(); // Adding key and value into the StringDictionary myDict.Add("G", "Geeks"); myDict.Add("F", "For"); myDict.Add("C", "C++"); myDict.Add("DS", "Data Structures"); myDict.Add("N", "Noida"); // Checking if "null" is contained in // StringDictionary myDict // This should raise "ArgumentNullException" // as the key is null if (myDict.ContainsKey(null)) Console.WriteLine("StringDictionary myDict contains the key"); else Console.WriteLine("StringDictionary myDict does not contain the key"); } } |
Runtime Error:
Unhandled Exception:
System.ArgumentNullException: Value cannot be null.
Parameter name: key
Note:
- The key is handled in a case-insensitive manner i.e, it is translated to lowercase before it is added to the string dictionary.
- This method is an O(1) operation.
Reference:
Recommended Posts:
- C# | Check if the StringDictionary contains a specific value
- C# | Check if the Hashtable contains a specific Key
- C# | Check if ListDictionary contains a specific key
- C# | Check if OrderedDictionary collection contains a specific key
- C# | Check whether a Hashtable contains a specific key or not
- C# | Check whether a SortedList object contains a specific key
- C# | Get the number of key/value pairs in the StringDictionary
- C# | Add key and value into StringDictionary
- C# | Remove entry with specified key from the StringDictionary
- C# | Get or set the value associated with the specified key in StringDictionary
- C# | Gets or sets the value at the specified key in StringDictionary
- C# | Check the HybridDictionary for a specific key
- C# | Check if the Hashtable contains a specific Value
- C# | Check if the SortedSet contains a specific element
- C# | Check if a SortedList object contains a specific value
- C# | Check if StringDictionary is synchronized (thread safe)
- C# | Check if two StringDictionary objects are equal or not
- C# | Check if SortedDictionary contains the specified key or not
- C# | Removing all entries from the StringDictionary
- C# | Copy StringDictionary to Array at the specified index
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

