C# | Get or set the value associated with specified key in SortedList Last Updated : 01 Feb, 2019 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 returns the value associated with the key parameter in the SortedList object if the key is found otherwise it returns null. Exceptions: ArgumentNullException: If the key is null. NotSupportedException: If the property is set and the SortedList object is read-only or if the property is set, the key doesn't exist in the collection and the SortedList has a fixed size. OutOfMemoryException: If there is not enough available memory to add the element to the SortedList. InvalidOperationException: If the comparer throws an exception. Below programs illustrate the use of above-discussed property: Example 1: CSharp // C# code to Gets or sets the value // associated with the specified key using System; using System.Collections; class GFG { // Driver code public static void Main() { // Creating a SortedList SortedList mylist = new SortedList(); // Adding elements in SortedList mylist.Add("g", "geeks"); mylist.Add("c", "c++"); mylist.Add("d", "data structures"); mylist.Add("q", "quiz"); // Get a collection of the keys. ICollection c = mylist.Keys; // Displaying the contents foreach(string str in c) Console.WriteLine(str + ": " + mylist[str]); // Setting the value associated with key "c" mylist["c"] = "C#"; Console.WriteLine("Updated Values:"); // Displaying the contents foreach(string str in c) Console.WriteLine(str + ": " + mylist[str]); } } Output: c: c++ d: data structures g: geeks q: quiz Updated Values: c: C# d: data structures g: geeks q: quiz Example 2: CSharp // C# code to Gets or sets the value // associated with the specified key using System; using System.Collections; class GFG { // Driver code public static void Main() { // Creating a SortedList SortedList mylist = new SortedList(); // Adding elements in SortedList mylist.Add("4", "Even"); mylist.Add("9", "Odd"); mylist.Add("5", "Odd and Prime"); mylist.Add("2", "Even and Prime"); // Get a collection of the keys. ICollection c = mylist.Keys; // Displaying the contents foreach(string str in c) Console.WriteLine(str + ": " + mylist[str]); // Setting the value associated // with key "56" which is not present // will result in the creation of // new key and value will be set which // is given by the user mylist["56"] = "New Value"; Console.WriteLine("Updated Values:"); // Displaying the contents foreach(string str in c) Console.WriteLine(str + ": " + mylist[str]); // Setting the value associated // with key "28" which is not present // will result in the creation of // new key and its value can be null mylist["28"] = null; Console.WriteLine("Updated Values:"); // Displaying the contents foreach(string str in c) Console.WriteLine(str + ": " + mylist[str]); } } Output: 2: Even and Prime 4: Even 5: Odd and Prime 9: Odd Updated Values: 2: Even and Prime 4: Even 5: Odd and Prime 56: New Value 9: Odd Updated Values: 2: Even and Prime 28: 4: Even 5: Odd and Prime 56: New Value 9: Odd Note: This property returns the value associated with the specific key. If that key is not found, and one is trying to get that, then this property will return null and if trying to set, it will result into the creation of a new element with the specified key. A key cannot be null, but a value can be. To distinguish between null that is returned because the specified key is not found and null that is returned because the value of the specified key is null, use the Contains method or the ContainsKey method to determine if the key exists in the list. Retrieving the value of this property is an O(log n) operation, where n is Count. Setting the property is an O(log n) operation if the key is already in the SortedList. If the key is not in the list, setting the property is an O(n) operation for unsorted data, or O(log n) if the new element is added at the end of the list. If insertion causes a resize, the operation is O(n). Reference: https://docs.microsoft.com/en-us/dotnet/api/system.collections.sortedlist.item?view=netframework-4.7.2 Comment More infoAdvertise with us Next Article C# | Get or Set the value associated with specified key in Hashtable K Kirti_Mangal Follow Improve Article Tags : C# CSharp-Collections-Namespace CSharp-Collections-SortedList Similar Reads C# | Get or Set the value associated with specified key in Hashtable Hashtable.Item[Object] Property is used to get or set the value associated with the specified key in the Hashtable. Syntax: public virtual object this[object key] { get; set; } Here, key is key of object type whose value is to get or set. Exceptions: ArgumentNullException: If the key is null. NotSup 3 min read C# | Get or set the value associated with specified key in ListDictionary ListDictionary.Item[Object] property is used to get or set the value associated with the specified key. Syntax: public object this[object key] { get; set; } Here, key is the key whose value to get or set. Return Value : The value associated with the specified key. If the specified key is not found, 2 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 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. R 3 min read 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# | Remove the element with the specified key from a SortedList 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.Remove(Object) method is used to remove the element with the specifi 3 min read Like