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

C# SortedList.IndexOfValue() Method: Here, we are going to learn how to get the index of the first occurrence of the specified value in a SortedList in C#.Net?
Submitted by Nidhi, on April 26, 2021

The IndexOfValue() method of SortedList class is used to get the index of the first occurrence of the specified value in a SortedList.

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.

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 . . .

C# SortedList Class Programs »





Comments and Discussions




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.