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.
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:
Index Keys Values  Fifth Manish  First Ram  Fourth Rohit  Second Shyam  Third Mohit The index of value 'Rohit' is: 2 The index of value 'Kirti' is: -1
Index Keys Values  1 C++  2 Java  3 DSA  4  5 C# The index of value 'null' is: 3
- The index sequence is based on the sort sequence. When an element is added, it is inserted into SortedList in the correct sort order, and the indexing adjusts accordingly. When an element is removed, the indexing also adjusts accordingly. So, the index of a specific key/value pair may change.
- The values of the elements of the SortedList are compared to the specified value using the Equals method.
- This method uses a linear search; therefore, this method is an O(n) operation, where n is Count.