SortedList.Contains(Object) Method is used to check whether a SortedList object contains a specific key.
Syntax:
public virtual bool Contains (object key);
Here, key is the Key which is to be located in the SortedList object.
Return Value: This method returns the true if the SortedList object contains an element with the specified key otherwise, it returns false
Exceptions:
- ArgumentNullException: If the key is null.
- InvalidOperationException: If the comparer throws an exception.
Below programs illustrate the use of above-discussed method:
Example 1:
using System;
using System.Collections;
class Geeks {
public static void Main(String[] args)
{
SortedList mylist = new SortedList();
mylist.Add( "1" , "C++" );
mylist.Add( "2" , "Java" );
mylist.Add( "3" , "DSA" );
mylist.Add( "4" , "Python" );
mylist.Add( "5" , "C#" );
Console.Write(mylist.Contains( "4" ));
}
}
|
Output:
True
Example 2:
using System;
using System.Collections;
class Geeks {
public static void Main(String[] args)
{
SortedList mylist = new SortedList();
mylist.Add( "First" , "Ram" );
mylist.Add( "Second" , "Shyam" );
mylist.Add( "Third" , "Mohit" );
mylist.Add( "Fourth" , "Rohit" );
mylist.Add( "Fifth" , "Manish" );
Console.Write(mylist.Contains( "Sixth" ));
}
}
|
Output:
False
Note:
- Contains implements
IDictionary.Contains
. It behaves exactly as ContainsKey.
- This method uses a binary search algorithm; therefore, this method is an O(log n) operation, where n is Count.
Reference:
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
01 Feb, 2019
Like Article
Save Article