C# | Check if ListDictionary contains a specific key
ListDictionary.Contains(Object) method is used to check whether the ListDictionary contains a specific key or not.
Syntax:
public bool Contains (object key);
Here, key is the key to locate in the ListDictionary.
Return Value: The method returns true if the ListDictionary contains an entry with the specified key, otherwise it returns false.
Exception: This method will give ArgumentNullException if the key is null.
Below given are some examples to understand the implementation in a better way:
Example 1:
// C# code to check if ListDictionary // contains a specific key using System; using System.Collections; using System.Collections.Specialized; class GFG { // Driver code public static void Main() { // Creating a ListDictionary named myDict ListDictionary myDict = new ListDictionary(); myDict.Add("Australia", "Canberra"); myDict.Add("Belgium", "Brussels"); myDict.Add("Netherlands", "Amsterdam"); myDict.Add("China", "Beijing"); myDict.Add("Russia", "Moscow"); myDict.Add("India", "New Delhi"); // Checking if ListDictionary contains // the key "Brazil" Console.WriteLine(myDict.Contains("Brazil")); } } |
Output:
False
Example 2:
// C# code to check if ListDictionary // contains a specific key using System; using System.Collections; using System.Collections.Specialized; class GFG { // Driver code public static void Main() { // Creating a ListDictionary named myDict ListDictionary myDict = new ListDictionary(); myDict.Add("Australia", "Canberra"); myDict.Add("Belgium", "Brussels"); myDict.Add("Netherlands", "Amsterdam"); myDict.Add("China", "Beijing"); myDict.Add("Russia", "Moscow"); myDict.Add("India", "New Delhi"); // Checking if ListDictionary contains // the key "null". This should raise // "ArgumentNullException" as the key is null Console.WriteLine(myDict.Contains(null)); } } |
Runtime Error:
Unhandled Exception:
System.ArgumentNullException: Key cannot be null.
Parameter name: key
Note: This method is an O(n) operation, where n is Count.
Reference:
Recommended Posts:
- C# | Check if ListDictionary is read-only
- C# | Check if ListDictionary has a fixed size
- C# | Check if two ListDictionary objects are equal
- C# | Check if ListDictionary is synchronized (thread safe)
- C# | Check if the StringDictionary contains a specific value
- C# | Check if the StringDictionary contains a specific key
- C# | Check the HybridDictionary for a specific key
- C# | Check whether a Hashtable contains a specific key or not
- C# | Check if the Hashtable contains a specific Key
- C# | Check if the Hashtable contains a specific Value
- C# | Check if the SortedSet contains a specific element
- C# | Check whether a SortedList object contains a specific key
- C# | Check if a SortedList object contains a specific value
- C# | Check if OrderedDictionary collection contains a specific key
- C# | Add the specified key and value into the ListDictionary
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.


