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 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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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]); 
    

chevron_right


Output:

c: c++
d: data structures
g: geeks
q: quiz
Updated Values:
c: C#
d: data structures
g: geeks
q: quiz

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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]); 
    

chevron_right


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:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.