Given a SortedList object, now our task is to check whether the given SortedList object contains the specific key or not. So to do this task we use ContainsKey() method. This method is used to determine whether a SortedList object contains a specific key or not. It will return true if the key is found, otherwise, it will return false.
Syntax:
bool SortedList.ContainsKey(object key);
Where key is located in the SortedList object.
Example:
Input : [(1, "Python"), (2, "c")]
Key : 1
Output : Found
Input : [(1, "Python"), (2, "c")]
Key : 4
Output : Not Found
Approach:
- Create a sorted list.
- Add key and values to the sorted list.
- Check whether the particular key exists in the list using ContainsKey() method.
- Display the output.
C#
using System;
using System.Collections;
class GFG{
static public void Main()
{
SortedList data = new SortedList();
data.Add(1, "Python" );
data.Add(2, "c" );
data.Add(3, "java" );
data.Add(4, "php" );
data.Add(5, "html" );
data.Add(6, "bigdata" );
data.Add(7, "java script" );
if (data.ContainsKey(1))
Console.WriteLine( "Present in the List" );
else
Console.WriteLine( "Not in the List" );
if (data.ContainsKey(4))
Console.WriteLine( "Present in the List" );
else
Console.WriteLine( "Not in the List" );
if (data.ContainsKey(8))
Console.WriteLine( "Present in the List" );
else
Console.WriteLine( "Not in the List" );
}
}
|
Output:
Present in the List
Present in the List
Not in the List