Skip to content
Related Articles

Related Articles

Improve Article

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

  • Last Updated : 06 Sep, 2021

SortedList class is a collection of (key, value) pairs which are sorted according to keys. Those pairs can be accessible by key and as well as by index(zero-based indexing). This comes under System.Collections namespace. SortedList.ContainsValue(Object) method is used to check whether a SortedList object contains a specific value or not.
 

Properties: 

  • A SortedList element can be accessed by its key or by its index.
  • A SortedList object internally maintains two arrays to store the elements of the list, i.e, one array for the keys and another array for the associated values.
  • A key cannot be null, but a value can be.
  • The capacity of a SortedList object is the number of elements the SortedList can hold.
  • A SortedList does not allow duplicate keys.
  • Operations on a SortedList object tend to be slower than operations on a Hashtable object because of the sorting.
  • Elements in this collection can be accessed using an integer index. Indexes in this collection are zero-based.

Syntax : 

public virtual bool ContainsValue (object value);

Here, value is the value to locate in the SortedList object and it can be null.
Return Value: This method returns True if the SortedList object contains an element with the specified value, otherwise it returns False.
Below programs illustrate the use of SortedList.ContainsValue(Object) Method:
Example 1:

CSHARP




// C# code to check if a SortedList
// object contains a specific value
using System;
using System.Collections;
 
class GFG {
 
    // Driver code
    public static void Main()
    {
 
        // Creating an SortedList
        SortedList mySortedList = new SortedList();
 
        // Adding elements to SortedList
        mySortedList.Add("1", "1st");
        mySortedList.Add("2", "2nd");
        mySortedList.Add("3", "3rd");
        mySortedList.Add("4", "4th");
 
        // Checking if a SortedList object
        // contains a specific value
        Console.WriteLine(mySortedList.ContainsValue(null));
    }
}
Output: 



False

 

Example 2:

CSHARP




// C# code to check if a SortedList
// object contains a specific value
using System;
using System.Collections;
 
class GFG {
 
    // Driver code
    public static void Main()
    {
 
        // Creating an SortedList
        SortedList mySortedList = new SortedList();
 
        // Adding elements to SortedList
        mySortedList.Add("h", "Hello");
        mySortedList.Add("g", "Geeks");
        mySortedList.Add("f", "For");
        mySortedList.Add("n", "Noida");
 
        // Checking if a SortedList object
        // contains a specific value
        Console.WriteLine(mySortedList.ContainsValue("Geeks"));
    }
}
Output: 
True

 

Note:  

  • The values of the elements of the SortedList object are compared to the specified value using the Equals method.
  • This method performs a linear search, therefore, the average execution time is proportional to Count, i.e, this method is an O(n) operation, where n is Count.

Reference:




My Personal Notes arrow_drop_up
Recommended Articles
Page :