Check if the StringDictionary contains a specific value in C#

The StringDictionary class in C# provides the ContainsValue() method to check if a specific value exists in the dictionary. This method returns true if the value is found, otherwise false. Note that StringDictionary automatically converts all keys to lowercase, but values remain case-sensitive.

Syntax

Following is the syntax for checking if a StringDictionary contains a specific value −

public virtual bool ContainsValue(string value)

Parameters

  • value ? The string value to locate in the StringDictionary. The value can be null.

Return Value

Returns true if the StringDictionary contains an element with the specified value; otherwise, false.

ContainsValue() Method Flow StringDictionary ContainsValue() Returns bool true/false Value comparison is case-sensitive

Using ContainsValue() Method

Example

using System;
using System.Collections;
using System.Collections.Specialized;

public class Demo {
    public static void Main(){
        StringDictionary strDict1 = new StringDictionary();
        strDict1.Add("A", "John");
        strDict1.Add("B", "Andy");
        strDict1.Add("C", "Tim");
        strDict1.Add("D", "Ryan");
        strDict1.Add("E", "Kevin");
        strDict1.Add("F", "Katie");
        strDict1.Add("G", "Brad");
        
        Console.WriteLine("StringDictionary1 elements...");
        foreach(DictionaryEntry d in strDict1){
            Console.WriteLine(d.Key + " " + d.Value);
        }
        
        Console.WriteLine("Does StringDictionary1 has key G? " + strDict1.ContainsKey("G"));
        Console.WriteLine("Does StringDictionary1 has value Ryan? " + strDict1.ContainsValue("Ryan"));
        Console.WriteLine("Does StringDictionary1 has value ryan? " + strDict1.ContainsValue("ryan"));
    }
}

The output of the above code is −

StringDictionary1 elements...
a John
b Andy
c Tim
d Ryan
e Kevin
f Katie
g Brad
Does StringDictionary1 has key G? True
Does StringDictionary1 has value Ryan? True
Does StringDictionary1 has value ryan? False

Checking for Non-Existent Values

Example

using System;
using System.Collections;
using System.Collections.Specialized;

public class Demo {
    public static void Main(){
        StringDictionary strDict1 = new StringDictionary();
        strDict1.Add("U", "Electronics");
        strDict1.Add("V", "Toys");
        strDict1.Add("W", "Books");
        strDict1.Add("X", "Accessories");
        
        Console.WriteLine("StringDictionary1 elements...");
        foreach(DictionaryEntry d in strDict1){
            Console.WriteLine(d.Key + " " + d.Value);
        }
        
        Console.WriteLine("Does StringDictionary1 has key G? " + strDict1.ContainsKey("G"));
        Console.WriteLine("Does StringDictionary1 has value Footwear? " + strDict1.ContainsValue("Footwear"));
        Console.WriteLine("Does StringDictionary1 has value Books? " + strDict1.ContainsValue("Books"));
        Console.WriteLine("Does StringDictionary1 has value null? " + strDict1.ContainsValue(null));
    }
}

The output of the above code is −

StringDictionary1 elements...
x Accessories
u Electronics
v Toys
w Books
Does StringDictionary1 has key G? False
Does StringDictionary1 has value Footwear? False
Does StringDictionary1 has value Books? True
Does StringDictionary1 has value null? False

Key Points

  • Case Sensitivity ? Values are case-sensitive, so "Ryan" and "ryan" are different values.

  • Key Conversion ? Keys are automatically converted to lowercase, but values maintain their original case.

  • Null Values ? The method can check for null values and returns appropriate results.

  • Performance ? ContainsValue() has O(n) time complexity as it searches through all values.

Conclusion

The ContainsValue()

Updated on: 2026-03-17T07:04:36+05:30

183 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements