The SortedList is related to System.Collections namespace. SortedList contains on key-value pairs. Where the keys are automatically sorted in ascending order. It is similar to a SortedList and is used when you need to maintain a collection of key-value pairs sorted by the keys.
To work with a SortedList, you can create one like this:
You can add key-value pairs to a SortedList using the Add method:
// inserting fruit items in key-value pair
// inserting fruit items in key-value pair
// inserting fruit items in key-value pair
// inserting fruit items in key-value pair
You can retrieve values from the SortedList using their associated keys:
// Returns 20
// Returns Violet
// Returns 20
// Returns Violet
You can check if a key exists in the SortedList using the Contains method:
// Returns true
// Returns true
// Returns true
// Returns true
You can use a foreach loop to iterate through the key-value pairs in a SortedList. The items are automatically sorted by the keys:
You can remove key-value pairs from the SortedList using the Remove method:
You can find the number of key-value pairs in the SortedList using the Count property:
// inserting fruit items in key-value pair
// inserting fruit items in key-value pair
// inserting fruit items in key-value pair
// inserting fruit items in key-value pair
SortedList is collection of Key-Value pairs. In Key-Value pairs, each key must be unique. SortedList can store specific type (int, float, string, bool) of data in sorted order.