Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Search in a SortedList object in C#
In C#, a SortedList is a collection that maintains key-value pairs sorted by keys. You can search for specific keys or values using built-in methods like ContainsKey() and ContainsValue(). The SortedList automatically keeps elements sorted, making searches efficient.
Syntax
Following are the key search methods for SortedList −
// Check if a key exists
bool ContainsKey(object key)
// Check if a value exists
bool ContainsValue(object value)
// Get value by key (returns null if not found)
object this[object key] { get; set; }
// Get index of a key (-1 if not found)
int IndexOfKey(object key)
// Get index of a value (-1 if not found)
int IndexOfValue(object value)
Using ContainsKey() and ContainsValue()
The most common search operations check for the existence of keys or 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", "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("Three"));
Console.WriteLine("The SortedList object has a fixed size? = " + list.IsFixedSize);
Console.WriteLine("Does the SortedList object contains key 10? = " + list.ContainsKey("10"));
}
}
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 The SortedList object has a fixed size? = False Does the SortedList object contains key 10? = False
Using IndexOfKey() and IndexOfValue()
You can also find the index position of keys and values in the sorted collection −
using System;
using System.Collections;
public class Demo {
public static void Main() {
SortedList sortedList = new SortedList();
sortedList.Add("A", "1");
sortedList.Add("B", "2");
sortedList.Add("C", "3");
sortedList.Add("D", "4");
sortedList.Add("E", "5");
Console.WriteLine("SortedList elements...");
foreach(DictionaryEntry d in sortedList) {
Console.WriteLine("Key = " + d.Key + ", Value = " + d.Value);
}
Console.WriteLine("Count of SortedList key-value pairs = " + sortedList.Count);
Console.WriteLine("Index of key 'C': " + sortedList.IndexOfKey("C"));
Console.WriteLine("Index of value '4': " + sortedList.IndexOfValue("4"));
Console.WriteLine("Index of key 'Z' (not found): " + sortedList.IndexOfKey("Z"));
Console.WriteLine("Does the SortedList object contains key M? = " + sortedList.ContainsKey("M"));
Console.WriteLine("Does the SortedList object contains key C? = " + sortedList.ContainsKey("C"));
}
}
The output of the above code is −
SortedList elements... Key = A, Value = 1 Key = B, Value = 2 Key = C, Value = 3 Key = D, Value = 4 Key = E, Value = 5 Count of SortedList key-value pairs = 5 Index of key 'C': 2 Index of value '4': 3 Index of key 'Z' (not found): -1 Does the SortedList object contains key M? = False Does the SortedList object contains key C? = True
Accessing Values by Key
You can directly access values using the key as an index. If the key doesn't exist, it returns null −
using System;
using System.Collections;
public class Demo {
public static void Main() {
SortedList list = new SortedList();
list.Add("apple", "fruit");
list.Add("carrot", "vegetable");
list.Add("banana", "fruit");
Console.WriteLine("SortedList contents:");
foreach(DictionaryEntry item in list) {
Console.WriteLine("{0} = {1}", item.Key, item.Value);
}
Console.WriteLine("\nSearching for specific items:");
Console.WriteLine("Value for 'apple': " + list["apple"]);
Console.WriteLine("Value for 'carrot': " + list["carrot"]);
Console.WriteLine("Value for 'grape' (doesn't exist): " + list["grape"]);
Console.WriteLine("Contains key 'banana': " + list.ContainsKey("banana"));
Console.WriteLine("Contains value 'vegetable': " + list.ContainsValue("vegetable"));
}
}
The output of the above code is −
SortedList contents: apple = fruit banana = fruit carrot = vegetable Searching for specific items: Value for 'apple': fruit Value for 'carrot': vegetable Value for 'grape' (doesn't exist): Contains key 'banana': True Contains value 'vegetable': True
Conclusion
Searching in a SortedList is efficient and straightforward using methods like ContainsKey(), ContainsValue(), IndexOfKey(), and IndexOfValue(). The automatic sorting of elements by keys ensures consistent ordering and enables efficient search operations for large collections.
