Open In App

C# | Getting index of the specified value in a SortedList object

SortedList.IndexOfValue(Object) Method is used to get the zero-based index of the first occurrence of the specified value in a SortedList object.

Syntax:



public virtual int IndexOfValue (object value);

Here, value is the Value which is to be located in the SortedList object. The value can be null.

Return Value: This method return the zero-based index of the first occurrence of the value parameter, if the value is found in the SortedList object otherwise it returns -1.



Below programs illustrate the use of above-discussed method:

Example 1:




// C# code to get the zero-based index 
// of the first occurrence of the specified
// value in a SortedList object
using System;
using System.Collections;
  
class Geeks {
  
    // Main Method
    public static void Main(String[] args)
    {
  
        // Creating a SortedList of integers
        SortedList mylist = new SortedList();
  
        // Adding elements to SortedList
        mylist.Add("First", "Ram");
        mylist.Add("Second", "Rohit");
        mylist.Add("Third", "Mohit");
          
        //taking value "Rohit" twice
        // but it give the first occurrence
        mylist.Add("Fourth", "Rohit");
          
        mylist.Add("Fifth", "Manish");
  
  
        // printing the keys and values of mylist
        Console.WriteLine("Index \t\t Keys \t\tValues");
  
        for (int i = 0; i < mylist.Count; i++) 
        {
            Console.WriteLine("[{0}]\t\t{1}\t\t{2}", i,
                mylist.GetKey(i), mylist.GetByIndex(i));
        }
          
        Console.Write("\nThe index of value 'Rohit' is: "); 
          
        // getting the index of value "Rohit"
        Console.Write(mylist.IndexOfValue("Rohit"));
          
        // getting the index of value which is
        // not present in mylist so it will
        // return -1
        Console.Write("\nThe index of value 'Kirti' is: "); 
        Console.Write(mylist.IndexOfValue("Kirti"));
    }
}

Output:

Index          Keys         Values
[0]        Fifth        Manish
[1]        First        Ram
[2]        Fourth        Rohit
[3]        Second        Shyam
[4]        Third        Mohit

The index of value 'Rohit' is: 2
The index of value 'Kirti' is: -1

Example 2:




// C# code to get the  zero-based index 
// of the first occurrence of the specified
// value in a SortedList object
using System;
using System.Collections;
  
class Geeks {
  
    // Main Method
    public static void Main(String[] args)
    {
  
        // Creating a SortedList of integers
        SortedList mylist = new SortedList();
  
        // Adding elements to SortedList
        mylist.Add("1", "C++");
        mylist.Add("2", "Java");
        mylist.Add("3", "DSA");
          
        // taking a value null
        mylist.Add("4", null);
          
        mylist.Add("5", "C#");
  
  
        // printing the keys and values of mylist
        Console.WriteLine("Index \t\t Keys \t\tValues");
  
        for (int i = 0; i < mylist.Count; i++) 
        {
            Console.WriteLine("[{0}]\t\t{1}\t\t{2}", i,
                mylist.GetKey(i), mylist.GetByIndex(i));
        }
          
        Console.Write("\nThe index of value 'null' is: "); 
          
        // getting the index of value "null"
        // it will give ArgumentNullException
        Console.Write(mylist.IndexOfValue(null));
    }
}

Output:

Index          Keys         Values
[0]        1        C++
[1]        2        Java
[2]        3        DSA
[3]        4        
[4]        5        C#

The index of value 'null' is: 3

Note:

Reference:


Article Tags :
C#