Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Get or set the value associated with specified key in SortedList in C#
In C#, the SortedList collection allows you to get or set values using the indexer syntax with square brackets. The indexer property list[key] provides both read and write access to values associated with specific keys in the sorted list.
Syntax
Following is the syntax for getting a value from SortedList −
object value = sortedList[key];
Following is the syntax for setting a value in SortedList −
sortedList[key] = value;
If the key exists, it updates the value. If the key doesn't exist, it adds a new key-value pair.
Getting Values from SortedList
You can retrieve values from a SortedList using the key as an index −
using System;
using System.Collections;
public class Demo {
public static void Main() {
SortedList list = new SortedList();
list.Add("A", "Books");
list.Add("B", "Electronics");
list.Add("C", "Appliances");
list.Add("D", "Pet Supplies");
list.Add("E", "Clothing");
list.Add("F", "Footwear");
Console.WriteLine("Value associated with key D = " + list["D"]);
Console.WriteLine("Value associated with key B = " + list["B"]);
Console.WriteLine("Value associated with key F = " + list["F"]);
}
}
The output of the above code is −
Value associated with key D = Pet Supplies Value associated with key B = Electronics Value associated with key F = Footwear
Setting Values in SortedList
You can update existing values or add new key-value pairs using the indexer −
using System;
using System.Collections;
public class Demo {
public static void Main() {
SortedList list = new SortedList();
list.Add("A", "Books");
list.Add("B", "Electronics");
list.Add("C", "Appliances");
list.Add("D", "Pet Supplies");
list.Add("E", "Clothing");
list.Add("F", "Footwear");
Console.WriteLine("Value associated with key E = " + list["E"]);
list["E"] = "HDD";
Console.WriteLine("Value associated with key E [Updated] = " + list["E"]);
// Adding a new key-value pair
list["G"] = "Sports";
Console.WriteLine("Value associated with key G [New] = " + list["G"]);
Console.WriteLine("Total items: " + list.Count);
}
}
The output of the above code is −
Value associated with key E = Clothing Value associated with key E [Updated] = HDD Value associated with key G [New] = Sports Total items: 7
Key Features
-
The indexer allows both getting and setting values by key.
-
If you try to get a value with a non-existent key, it throws a
KeyNotFoundException. -
If you set a value with a new key, it automatically adds the key-value pair to the collection.
-
Keys are maintained in sorted order automatically.
Conclusion
The SortedList indexer property provides a convenient way to access and modify values by key. Use list[key] to get values and list[key] = value to set or update values, with automatic sorting maintained throughout operations.
