Sorted lists

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

Get C# Data Structures and Algorithms now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.