Q:

C# program to get the index of the specified key in a SortedList (Example of IndexOfKey() Method)

belongs to collection: C# SortedList Class Programs

0

Syntax:

    int SortedList.IndexOfKey(object key);

Parameter(s):

  • key: The key whose index to be found.

Return value:

It returns a zero-based index of the given key object, if the given key object is not found in the SortedList object it returns -1.

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program:

The source code to get the index of the specified key in a SortedList is given below. The given program is compiled and executed successfully.

using System;
using System.Collections;

class SortedListEx
{
    //Entry point of Program
    static public void Main()
    {
        //Creation of SortedList object
        SortedList list = new SortedList();
        
        int index = 0;


        //Add elements to SortedList 
        list.Add(101, "India");
        list.Add(105, "America");
        list.Add(102, "Austrelia");
        list.Add(103, "Africa");
        list.Add(104, "Canada");
                
        Console.WriteLine("Index of keys:");
        foreach (int key in list.GetKeyList())
        {
            index = list.IndexOfKey(key);
            Console.WriteLine("Key "+key+" at index: "+index);
        }

    }
}

Output:

Index of keys:
Key 101 at index: 0
Key 102 at index: 1
Key 103 at index: 2
Key 104 at index: 3
Key 105 at index: 4
Press any key to continue . . .

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C# SortedList Class Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C# program to get the index of the first occurrenc... >>
<< C# program to get the values in a SortedList (Exam...