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
Getting the index of the specified key in a SortedList object in C#
To get the index of a specified key in a SortedList object, use the IndexOfKey() method. This method returns the zero-based index of the key within the sorted collection, or -1 if the key is not found.
Syntax
Following is the syntax for the IndexOfKey() method −
public virtual int IndexOfKey(object key)
Parameters
- key ? The key to locate in the SortedList object.
Return Value
Returns the zero-based index of the key parameter if found; otherwise, returns -1.
Example
The following example demonstrates how to find the index of a specific key in a SortedList −
using System;
using System.Collections;
public class Demo {
public static void Main(String[] args) {
SortedList list = new SortedList();
list.Add("One", "Finance");
list.Add("Two", "Marketing");
list.Add("Three", "Sales");
list.Add("Four", "Purchase");
list.Add("Five", "Operations");
list.Add("Six", "IT");
Console.WriteLine("SortedList elements...");
foreach(DictionaryEntry d in list) {
Console.WriteLine(d.Key + " " + d.Value);
}
Console.WriteLine("\nIndex at key One = " + list.IndexOfKey("One"));
Console.WriteLine("Index at key Five = " + list.IndexOfKey("Five"));
Console.WriteLine("Index at key NonExistent = " + list.IndexOfKey("NonExistent"));
}
}
The output of the above code is −
SortedList elements... Five Operations Four Purchase One Finance Six IT Three Sales Two Marketing Index at key One = 2 Index at key Five = 0 Index at key NonExistent = -1
Using IndexOfKey() with Different Data Types
The following example shows how IndexOfKey() works with different SortedList collections −
using System;
using System.Collections;
public class Demo {
public static void Main(String[] args) {
// SortedList with string keys and integer values
SortedList list1 = new SortedList();
list1.Add("One", 1);
list1.Add("Two", 2);
list1.Add("Three", 3);
list1.Add("Four", 4);
list1.Add("Five", 5);
Console.WriteLine("SortedList1 elements...");
foreach(DictionaryEntry d in list1) {
Console.WriteLine(d.Key + " " + d.Value);
}
Console.WriteLine("Index at key Five = " + list1.IndexOfKey("Five"));
// SortedList with single character keys
SortedList list2 = new SortedList();
list2.Add("A", "Accessories");
list2.Add("B", "Books");
list2.Add("C", "Smart Wearable Tech");
list2.Add("D", "Home Appliances");
Console.WriteLine("\nSortedList2 elements...");
foreach(DictionaryEntry d in list2) {
Console.WriteLine(d.Key + " " + d.Value);
}
Console.WriteLine("Index at key B = " + list2.IndexOfKey("B"));
}
}
The output of the above code is −
SortedList1 elements... Five 5 Four 4 One 1 Three 3 Two 2 Index at key Five = 0 SortedList2 elements... A Accessories B Books C Smart Wearable Tech D Home Appliances Index at key B = 1
How It Works
The IndexOfKey() method performs a binary search on the sorted keys to efficiently locate the specified key. Since SortedList maintains its elements in sorted order by key, the index represents the position of the key in this sorted sequence.
Conclusion
The IndexOfKey() method provides an efficient way to find the position of a key in a SortedList. It returns the zero-based index if the key exists, or -1 if not found. Since SortedList maintains sorted order, the index reflects the key's position in the alphabetically sorted sequence.
