C# | Getting index of the specified value in a SortedList object Last Updated : 01 Feb, 2019 Comments Improve Suggest changes Like Article Like Report 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: CSharp // 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: CSharp // 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: 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. Reference: https://docs.microsoft.com/en-us/dotnet/api/system.collections.sortedlist.indexofvalue?view=netframework-4.7.2 Comment More infoAdvertise with us Next Article C# | Getting index of the specified value in a SortedList object K Kirti_Mangal Follow Improve Article Tags : C# CSharp-method CSharp-Collections-Namespace CSharp-Collections-SortedList Similar Reads C# | Getting the value at the specified index of a SortedList object SortedList.GetByIndex(Int32) Method is used to get the value at the specified index of a SortedList object. Syntax: public virtual object GetByIndex (int index); Here index is the zero-based index of the value to get. Return Value: It returns the value at the specified index of the SortedList object 2 min read C# | Getting the index of the specified key in a SortedList object SortedList.IndexOfKey(Object) Method is used to get the zero-based index of the specified key in a SortedList object. Syntax: public virtual int IndexOfKey (object key); Here, key is the Key which is to be located in the SortedList object. Return Value: This method returns the zero-based index of ty 3 min read C# | Getting the key at the specified index of a SortedList object SortedList.GetKey(Int32) Method is used to get the key at the specified index of a SortedList object. Syntax: public virtual object GetKey (int index); Here, index is the zero-based index of the key to get. Return Value: This method returns the key at the specified index of the SortedList object. Ex 2 min read C# | Getting the Values in a SortedList object SortedList.Values Property is used to get the values in a SortedList object. Syntax: public virtual System.Collections.ICollection Values { get; } Property Value: An ICollection object containing the values in the SortedList object. Below programs illustrate the use of above-discussed property: Exam 2 min read C# | Replacing the value at a specific index in a SortedList object SortedList.SetByIndex(Int32, Object) Method is used to replace the value at a specific index in a SortedList object. Syntax: public virtual void SetByIndex (int index, object value); Parameters: index: It is the zero-based index at which to save value. value: It is the Object to save into the Sorted 3 min read C# | Getting the list of Values of a SortedList object SortedList.GetValueList Method is used to get the list of keys in a SortedList object. Syntax: public virtual System.Collections.IList GetValueList (); Return Value: It returns an IList object containing the values in the SortedList object. Below programs illustrate the use of above-discussed method 2 min read C# | Getting the keys in a SortedList object SortedList.Keys Property is used to get the keys in a SortedList object. Syntax: public virtual System.Collections.ICollection Keys { get; } Property Value: An ICollection object containing the keys in the SortedList object. Below programs illustrate the use of above-discussed property: Example 1: C 2 min read C# | Get or set the value associated with specified key in SortedList SortedList.Item[Object] Property is used to get and set the value associated with a specific key in a SortedList object. Syntax: public virtual object this[object key] { get; set; } Here, the key is associated with the value to get or set. It is of the object type. Return Value: This property return 4 min read C# | Check if a SortedList object contains a specific value SortedList class is a collection of (key, value) pairs which are sorted according to keys. Those pairs can be accessible by key and as well as by index(zero-based indexing). This comes under System.Collections namespace. SortedList.ContainsValue(Object) method is used to check whether a SortedList o 2 min read C# | Getting the list of keys of a SortedList object SortedList.GetKeyList Method is used to get the list of keys in a SortedList object. Syntax: public virtual System.Collections.IList GetKeyList (); Return Value: It returns an IList object containing the keys in the SortedList object. Below programs illustrate the use of above-discussed method: Exam 2 min read Like