C# - SortedList.IndexOfValue() Method with Example

In this tutorial, we will learn about the C# SortedList.IndexOfValue() method with its definition, usage, syntax, and example. By Nidhi Last updated : March 31, 2023

SortedList.IndexOfValue() Method

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


int SortedList.IndexOfValue(object? value);


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

C# Example of SortedList.IndexOfValue() Method

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);



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!

Load comments ↻

Copyright © 2024 www.includehelp.com. All rights reserved.