O'Reilly logo

From Java to C#: A Developer's Guide by Heng Ngee Mok

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

19.5. SortedList

Put simply, a SortedList is a Hashtable which is sorted by the keys. Like a Hashtable, it is accessible by both its keys and values. The API documentation describes a SortedList as a hybrid between a Hashtable and an Array. It behaves like a Hashtable when accessed via a key. It behaves like an Array when accessed via its GetByIndex method.

If you do not need to access elements in a SortedList via its indices, then use a Hashtable rather than a SortedList because the sorting has a performance implication, especially if you do not need that functionality.

The program below demonstrates how a SortedList can be used. The output is interspersed with the code to show the outputs of the different sections.

 1: using System; 2: using ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required