ContainsValue in C#

The ContainsValue() method in C# is used to determine whether a Dictionary contains a specific value. It returns true if the value exists in the dictionary, and false otherwise. This method performs a sequential search through all values in the dictionary.

Syntax

Following is the syntax for using the ContainsValue() method −

bool result = dictionary.ContainsValue(value);

Parameters

  • value − The value to search for in the dictionary. The type must match the value type of the dictionary.

Return Value

Returns true if the dictionary contains the specified value; otherwise, false.

Using ContainsValue() with String Dictionary

Here's how to use ContainsValue() to search for string values in a dictionary −

using System;
using System.Collections.Generic;

class Program {
   static void Main() {
      Dictionary<string, string> devices = new Dictionary<string, string>();
      devices.Add("keyboard", "Input Device");
      devices.Add("mouse", "Pointer Device");
      devices.Add("monitor", "Display Device");
      
      // Search for specific values
      Console.WriteLine("Contains 'Input Device': " + devices.ContainsValue("Input Device"));
      Console.WriteLine("Contains 'Audio Device': " + devices.ContainsValue("Audio Device"));
      Console.WriteLine("Contains 'Display Device': " + devices.ContainsValue("Display Device"));
   }
}

The output of the above code is −

Contains 'Input Device': True
Contains 'Audio Device': False
Contains 'Display Device': True

Using ContainsValue() with Integer Dictionary

The method works with different data types. Here's an example with integer values −

using System;
using System.Collections.Generic;

class Program {
   static void Main() {
      Dictionary<string, int> scores = new Dictionary<string, int>();
      scores.Add("Alice", 95);
      scores.Add("Bob", 87);
      scores.Add("Charlie", 92);
      
      // Check if certain scores exist
      Console.WriteLine("Score 95 exists: " + scores.ContainsValue(95));
      Console.WriteLine("Score 100 exists: " + scores.ContainsValue(100));
      Console.WriteLine("Score 87 exists: " + scores.ContainsValue(87));
   }
}

The output of the above code is −

Score 95 exists: True
Score 100 exists: False
Score 87 exists: True

Practical Example with Conditional Logic

You can use ContainsValue() in conditional statements to perform actions based on whether a value exists −

using System;
using System.Collections.Generic;

class Program {
   static void Main() {
      Dictionary<string, string> inventory = new Dictionary<string, string>();
      inventory.Add("apple", "fruit");
      inventory.Add("carrot", "vegetable");
      inventory.Add("bread", "grain");
      
      string searchCategory = "fruit";
      
      if (inventory.ContainsValue(searchCategory)) {
         Console.WriteLine("We have items in the '" + searchCategory + "' category.");
      } else {
         Console.WriteLine("No items found in the '" + searchCategory + "' category.");
      }
      
      string missingCategory = "dairy";
      if (inventory.ContainsValue(missingCategory)) {
         Console.WriteLine("We have items in the '" + missingCategory + "' category.");
      } else {
         Console.WriteLine("No items found in the '" + missingCategory + "' category.");
      }
   }
}

The output of the above code is −

We have items in the 'fruit' category.
No items found in the 'dairy' category.

Key Considerations

  • PerformanceContainsValue() has O(n) time complexity as it searches through all values sequentially.

  • Case Sensitivity − For string values, the search is case-sensitive by default.

  • Null Values − The method can search for null values if the dictionary allows them.

Conclusion

The ContainsValue() method provides a straightforward way to check if a specific value exists in a Dictionary. While it performs a linear search through all values, it's useful for validation logic and conditional operations based on dictionary contents.

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

142 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements