Q:

C# program to get the value at the specified index from a SortedList (Example of GetByIndex() Method)

belongs to collection: C# SortedList Class Programs

0

Syntax:

    object SortedList.GetByIndex(int index);

Parameter(s):

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

Return value:

The value 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 value at the specified index from 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();

        string value;

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

        Console.WriteLine("List accessed by index:");
        for(int index=0; index<=4; index++)
        {
            value = list.GetByIndex(index).ToString();
            Console.WriteLine("\t"+value);
        }
        Console.WriteLine();

    }
}

Output:

List accessed by index:
        India
        Australia
        Africa
        Canada
        America

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 key at the specified index o... >>
<< C# program to remove an element at the specified i...