C# | Getting the index of the specified key in a SortedList object

SortedList.IndexOfKey(Object) Method is used to get the zero-based index of the specified key in a SortedList object.

Syntax:

public virtual int IndexOfKey (object key);

Here, key is the Key which is to be located in the SortedList object.

Return Value: This method returns the zero-based index of type System.Int32 of the key parameter if the key is found in the SortedList object otherwise it returns -1.

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# code to get the zero-based index
// of the specified key in a SortedList
// object
using System;
using System.Collections;
  
class Geeks {
  
    // Main Method
    public static void Main(String[] args)
    {
  
        // Creating a SortedList of integers
        SortedList mylist = new SortedList();
  
        // Adding elements to SortedList
        mylist.Add("First", "Ram");
        mylist.Add("Second", "Shyam");
        mylist.Add("Third", "Mohit");
        mylist.Add("Fourth", "Rohit");
        mylist.Add("Fifth", "Manish");
  
  
        // printing the keys and values of mylist
        Console.WriteLine("Index \t\t Keys \t\tValues");
  
        for (int i = 0; i < mylist.Count; i++) 
        {
            Console.WriteLine("[{0}]\t\t{1}\t\t{2}", i,
                mylist.GetKey(i), mylist.GetByIndex(i));
        }
          
        Console.Write("\nThe index of key 'Third' is: "); 
          
        // getting the index of key "Third"
        Console.Write(mylist.IndexOfKey("Third"));
          
        // getting the index of key which is
        // not present in mylist so it will
        // return -1
        Console.Write("\nThe index of key 'Sixth' is: "); 
        Console.Write(mylist.IndexOfKey("Sixth"));
    }
}

chevron_right


Output:

Index          Keys         Values
[0]        Fifth        Manish
[1]        First        Ram
[2]        Fourth        Rohit
[3]        Second        Shyam
[4]        Third        Mohit

The index of key 'Third' is: 4
The index of key 'Sixth' is: -1

Example 2: To demonstrate the case where ArgumentNullException can occur

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# code to get the zero-based index
// of the specified key in a SortedList
// object
using System;
using System.Collections;
  
class Geeks {
  
    // Main Method
    public static void Main(String[] args)
    {
  
        // Creating a SortedList of integers
        SortedList mylist = new SortedList();
  
        // Adding elements to SortedList
        mylist.Add("1", "C++");
        mylist.Add("2", "Java");
        mylist.Add("3", "DSA");
        mylist.Add("4", "Python");
        mylist.Add("5", "C#");
  
  
        // printing the keys and values of mylist
        Console.WriteLine("Index \t\t Keys \t\tValues");
   
        for (int i = 0; i < mylist.Count; i++) 
        {
            Console.WriteLine("[{0}]\t\t{1}\t\t{2}", i,
                mylist.GetKey(i), mylist.GetByIndex(i));
        }
          
        Console.Write("\nThe index of key 'null' is: ");    
          
        // getting the index of key "null"
        // it will give ArgumentNullException
        Console.Write(mylist.IndexOfKey(null));
    }
}

chevron_right


Runtime Error:

Unhandled Exception:
System.ArgumentNullException: Key cannot be null.
Parameter name: key

Note:

  • The index sequence is based on the sort sequence. When an element is added, it is inserted into SortedList in the correct sort order, and the indexing adjusts accordingly. When an element is removed, the indexing also adjusts accordingly. So, the index of a specific key/value pair may change.
  • This method uses a binary search algorithm so, this method is an O(log n) operation, where n is the Count.

Reference:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.