Javatpoint Logo
Javatpoint Logo

SortedList.IndexOfValue(Object) Method in C#

In this article, we will discuss the SortedList.IndexOfValue(Object) Method in C# with its syntax, parameters, and examples.

What is the SortedList.IndexOfValue(Object) Method?

The IndexOfValue() method will returns the zero based index of the first occurrence of the specified value in a SortedList object.

Syntax:

It has the following syntax:

The parameters taken in the method are value and object. This function will return the zero-based index of the first occurrence of the value parameter if the value is found in the sorted list object. Otherwise, it will return -1.

This method will iterate through the values in the sorted list and compares them with the specified value using the default equality comparer for the value type. It stops iterating and will return the index of the first occurrence it finds. This method uses O(n) time complexity, where n is the number of elements in the given sorted list due to its linear iteration through the sorted list.

Example:

Let us take a program to illustrate the IndexOfValue() method in C#.

Output:

SortedList.IndexOfValue(Object) Method in C#

Explanation:

The above program illustrates the use of the IndexOfValue() method with the sorted list. Firstly, SortedList is initialized with an integer as the key and a string as the value. After that, a while loop takes the input from the user. It takes the user's number and corresponding fruit name and adds it to the sorted list. The while loop will break if the input the user gives is equal. After that, a variable index is initialized, and the result of the IndexOfValue() method is stored in that variable. This index is displayed. Then again, a foreach loop is used to iterate through the SortedList and print according to the index.

Example 2:

Let us take another program where a sorted list uses complex objects as values in C#.

Output:

SortedList.IndexOfValue(Object) Method in C#

Explanation:

This program is also used to demonstrate the IndexOfValue() method. Initially, a SortedList is declared and initialized with some key-value pairs sorted by keys. Keys are integers, and values are the names of the people. A while loop is used to take the input from the user. The input is the name and age of the person. A variablePerson is created to store the index of the person whose age iPersonIf it is present in the sortedList, and then the index is given. Otherwise, it is given -1. In this class, a class is defined as a person. In conclusion, this program demonstrated using a SortedList to store and manipulate key-value pairs.

Conclusion:

In conclusion, the IndexOfValue() method will provide an efficient way to retrieve the first occurrence of the value in the SortedList. It gives the index to the key value pair collection. This method will go through all the values in the sorted list and compare them with the specific value. If the value is present in the SortedList, it will return the zero-based index of the found value; otherwise, it will return the -1. The time complexity of this method is O(n). This method will enhance the functionality of the SortedList.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA