Check if a SortedList object contains a specific value in C#

The SortedList class in C# provides the ContainsValue() method to check if a specific value exists in the collection. This method returns true if the value is found, and false otherwise.

Syntax

Following is the syntax for the ContainsValue() method −

public virtual bool ContainsValue(object value)

Parameters

  • value − The value to locate in the SortedList. The value can be null.

Return Value

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

Using ContainsValue() - Value Found

The following example demonstrates checking for a value that exists in the SortedList −

using System;
using System.Collections;

public class Demo {
   public static void Main(){
      SortedList list = new SortedList();
      list.Add("1", "One");
      list.Add("2", "Two");
      list.Add("3", "Three");
      list.Add("4", "Four");
      list.Add("5", "Five");
      list.Add("6", "Six");
      list.Add("7", "Seven");
      list.Add("8", "Eight");
      
      Console.WriteLine("Key and Value of SortedList....");
      foreach(DictionaryEntry k in list )
         Console.WriteLine("Key: {0}, Value: {1}", k.Key , k.Value );
      
      Console.WriteLine("Is the SortedList having the value? " + list.ContainsValue("Five"));
   }
}

The output of the above code is −

Key and Value of SortedList....
Key: 1, Value: One
Key: 2, Value: Two
Key: 3, Value: Three
Key: 4, Value: Four
Key: 5, Value: Five
Key: 6, Value: Six
Key: 7, Value: Seven
Key: 8, Value: Eight
Is the SortedList having the value? True

Using ContainsValue() - Value Not Found

The following example demonstrates checking for a value that does not exist in the SortedList −

using System;
using System.Collections;

public class Demo {
   public static void Main(){
      SortedList list = new SortedList();
      list.Add("1", "One");
      list.Add("2", "Two");
      list.Add("3", "Three");
      list.Add("4", "Four");
      list.Add("5", "Five");
      list.Add("6", "Six");
      list.Add("7", "Seven");
      list.Add("8", "Eight");
      
      Console.WriteLine("Key and Value of SortedList....");
      foreach(DictionaryEntry k in list )
         Console.WriteLine("Key: {0}, Value: {1}", k.Key , k.Value );
      
      Console.WriteLine("Is the SortedList having the value? " + list.ContainsValue("Tenth"));
   }
}

The output of the above code is −

Key and Value of SortedList....
Key: 1, Value: One
Key: 2, Value: Two
Key: 3, Value: Three
Key: 4, Value: Four
Key: 5, Value: Five
Key: 6, Value: Six
Key: 7, Value: Seven
Key: 8, Value: Eight
Is the SortedList having the value? False

Checking for Null Values

The ContainsValue() method can also check for null values in the SortedList −

using System;
using System.Collections;

public class Demo {
   public static void Main(){
      SortedList list = new SortedList();
      list.Add("1", "One");
      list.Add("2", null);
      list.Add("3", "Three");
      
      Console.WriteLine("Checking for null value: " + list.ContainsValue(null));
      Console.WriteLine("Checking for existing value: " + list.ContainsValue("One"));
   }
}

The output of the above code is −

Checking for null value: True
Checking for existing value: True

Conclusion

The ContainsValue() method provides an efficient way to check if a specific value exists in a SortedList. It performs a linear search through the values and returns a boolean result, making it useful for validating data presence before performing operations.

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

203 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements