Open In App

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:

Below programs illustrate the use of above-discussed property:

Example 1:




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




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

Reference:


Article Tags :
C#