C# | Check if the StringDictionary contains a specific value
StringDictionary.ContainsValue(String) method is used to check whether the StringDictionary contains a specific value or not.
Syntax:
public virtual bool ContainsValue (string value);
Here, value is the value to locate in the StringDictionary. The value can be null.
Return Value: The method returns true if the StringDictionary contains an element with the specified value, otherwise it returns false.
Below programs illustrate the use of StringDictionary.ContainsValue(String) method:
Example 1:
// C# code to check if the // StringDictionary contains // a specific value 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 "C++" is contained in // StringDictionary myDict if (myDict.ContainsValue("C++")) Console.WriteLine("StringDictionary myDict contains the value"); else Console.WriteLine("StringDictionary myDict does not contain the value"); } } |
StringDictionary myDict contains the value
Example 2:
// C# code to check if the // StringDictionary contains // a specific value 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 // It should not raise any exception if (myDict.ContainsValue(null)) Console.WriteLine("StringDictionary myDict contains the value"); else Console.WriteLine("StringDictionary myDict does not contain the value"); } } |
StringDictionary myDict does not contain the value
Note:
- The values of the elements of the StringDictionary are compared to the specified value using the Object.Equals method.
- This method is an O(n) operation, where n is Count.
Reference:
Recommended Posts:
- C# | Check if the StringDictionary contains a specific key
- C# | Check if two StringDictionary objects are equal or not
- C# | Check if StringDictionary is synchronized (thread safe)
- C# | Check whether a Hashtable contains a specific key or not
- C# | Check the HybridDictionary for a specific key
- C# | Check if the Hashtable contains a specific Value
- C# | Check if ListDictionary contains a specific key
- C# | Check if the Hashtable contains a specific Key
- C# | Check if the SortedSet contains a specific element
- C# | Check if a SortedList object contains a specific value
- C# | Check if OrderedDictionary collection contains a specific key
- C# | Check whether a SortedList object contains a specific key
- C# | Get or set the value associated with the specified key in StringDictionary
- C# | Add key and value into StringDictionary
- How to create a StringDictionary in C#
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.


