SortedDictionary.ContainsValue() Method in C#

The SortedDictionary.ContainsValue() method in C# is used to determine whether the SortedDictionary<TKey, TValue> contains an element with the specified value. This method performs a linear search through all values in the collection.

Syntax

Following is the syntax of the ContainsValue() method −

public bool ContainsValue(TValue value)

Parameters

value − The value to be searched in the SortedDictionary<TKey, TValue>. The value can be null for reference types.

Return Value

Returns true if the SortedDictionary<TKey, TValue> contains an element with the specified value; otherwise, false.

Using ContainsValue() to Check Existing Values

The following example demonstrates searching for existing values in a SortedDictionary −

using System;
using System.Collections.Generic;

public class Demo {
    public static void Main() {
        SortedDictionary<int, string> sortedDict = new SortedDictionary<int, string>();
        sortedDict.Add(100, "Inspiron");
        sortedDict.Add(200, "Alienware");
        sortedDict.Add(300, "Projectors");
        sortedDict.Add(400, "XPS");
        
        Console.WriteLine("SortedDictionary key-value pairs:");
        foreach (var kvp in sortedDict) {
            Console.WriteLine($"Key = {kvp.Key}, Value = {kvp.Value}");
        }
        
        Console.WriteLine($"Count = {sortedDict.Count}");
        
        // Test existing value
        bool hasNotebook = sortedDict.ContainsValue("Inspiron");
        Console.WriteLine($"Contains 'Inspiron': {hasNotebook}");
        
        // Test non-existing value
        bool hasTablet = sortedDict.ContainsValue("Tablet");
        Console.WriteLine($"Contains 'Tablet': {hasTablet}");
    }
}

The output of the above code is −

SortedDictionary key-value pairs:
Key = 100, Value = Inspiron
Key = 200, Value = Alienware
Key = 300, Value = Projectors
Key = 400, Value = XPS
Count = 4
Contains 'Inspiron': True
Contains 'Tablet': False

Using ContainsValue() with Null Values

The method can handle null values for reference types. Here's an example −

using System;
using System.Collections.Generic;

public class Demo {
    public static void Main() {
        SortedDictionary<int, string> sortedDict = new SortedDictionary<int, string>();
        sortedDict.Add(1, "One");
        sortedDict.Add(2, null);
        sortedDict.Add(3, "Three");
        
        Console.WriteLine("SortedDictionary contents:");
        foreach (var kvp in sortedDict) {
            string value = kvp.Value ?? "null";
            Console.WriteLine($"Key = {kvp.Key}, Value = {value}");
        }
        
        // Check for null value
        bool hasNull = sortedDict.ContainsValue(null);
        Console.WriteLine($"Contains null: {hasNull}");
        
        // Check for existing and non-existing values
        Console.WriteLine($"Contains 'One': {sortedDict.ContainsValue("One")}");
        Console.WriteLine($"Contains 'Four': {sortedDict.ContainsValue("Four")}");
    }
}

The output of the above code is −

SortedDictionary contents:
Key = 1, Value = One
Key = 2, Value = null
Key = 3, Value = Three
Contains null: True
Contains 'One': True
Contains 'Four': False

How It Works

The ContainsValue() method performs a linear search through all values in the SortedDictionary, comparing each value with the specified search value using the default equality comparer for the value type. This operation has O(n) time complexity, where n is the number of elements.

ContainsValue() Search Process Key: 100 "Inspiron" Key: 200 "Alienware" Key: 300 "Projectors" Key: 400 "XPS" Search: "Alienware" FOUND! Returns true Linear search compares each value until match found

Common Use Cases

  • Validating if a specific value exists before performing operations

  • Implementing search functionality in applications

  • Data validation and integrity checks

  • Filtering operations based on value existence

Conclusion

The ContainsValue() method provides a straightforward way to search for values in a SortedDictionary. While it has O(n) complexity due to linear search, it's useful for validation and search operations where you need to verify value existence before proceeding with other operations.

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

134 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements