Q:

C# program to get the index of the first occurrence of the specified value in a SortedList (Example of IndexOfValue() Method)

belongs to collection: C# SortedList Class Programs

0

Syntax:

    int SortedList.IndexOfValue(object? value);

Parameter(s):

  • value: The value whose index to be found or it can be null also.

Return value:

It returns the index of the first occurrence of the given value, if the given value 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 first occurrence of the specified value 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 Values:");
        foreach (string value in list.GetValueList())
        {
            index = list.IndexOfValue(value);
            Console.WriteLine("Value ""+value+"" at index: "+index);
        }

    }
}

Output:

Index of Values:
Value "India    " at index: 0
Value "Austrelia" at index: 1
Value "Africa   " at index: 2
Value "Canada   " at index: 3
Value "America  " 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 check whether a SortedList object co... >>
<< C# program to get the index of the specified key i...