In this chapter, you have already learned how to store data using arrays and lists. However, do you know that you can even use a data structure that ensures that the elements are sorted? If not, let's get to know the SortedList
generic class (from the System.Collections.Generic
namespace), which is a collection of key-value pairs, sorted by keys, without the necessity of sorting them on your own. It is worth mentioning that all keys must be unique and cannot be equal to null
.
You can easily add an element to the collection using the Add
method, and remove a specified item using the Remove
method. Among other methods, it is worth noting ContainsKey
and ContainsValue
for checking whether the collection contains an item with a given key or value, as well as IndexOfKey
and IndexOfValue
for returning an index of a given key or value within the collection. As the sorted list stores the key-value pairs, you have also access to the Keys
and Values
properties. Particular keys and values...