Skip to content
Related Articles

Related Articles

Improve Article

C# | Check if the StringDictionary contains a specific value

  • Last Updated : 01 Feb, 2019

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");
    }
}
Output:
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");
    }
}
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
Recommended Articles
Page :