Q:

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

belongs to collection: C# SortedList Class Programs

0

Syntax:

    object SortedList.GetKey(int index);

Parameter(s):

  • index: The zero-based index of the key to get.

Return value:

It returns the key at the specified index of the SortedList.

Exception(s):

  • System.ArgumentOutOfRangeException
  •  

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 key at the specified index of 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 key;

        //Add elements to SortedList 
        list.Add(1, "India");
        list.Add(5, "America");
        list.Add(2, "Austrelia");
        list.Add(3, "Africa");
        list.Add(4, "Canada");

        Console.WriteLine("keys by index:");
        for(int index=0; index<=4; index++)
        {
            key = Convert.ToInt32(list.GetKey(index));
            Console.WriteLine(key);
        }
        Console.WriteLine();

    }
}

Output:

keys by index:
1
2
3
4
5
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 keys in a SortedList (Exampl... >>
<< C# program to get the value at the specified index...