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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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");
    }
}

chevron_right


Output:

StringDictionary myDict contains the value

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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");
    }
}

chevron_right


Output:

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:



My Personal Notes arrow_drop_up

In love with a semicolon because sometimes i miss it so badly)

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.