Export (0) Print
Expand All
3 out of 6 rated this helpful - Rate this topic

SortedList and SortedDictionary Collection Types 

The System.Collections.SortedList class, the System.Collections.Generic.SortedList generic class, and the System.Collections.Generic.SortedDictionary generic class are similar to the Hashtable class and the Dictionary generic class in that they implement the IDictionary interface, but they maintain their elements in sort order by key, and they do not have the O(1) insertion and retrieval characteristic of hash tables. The three classes have several features in common:

  • All three classes implement the System.Collections.IDictionary interface. The two generic classes also implement the System.Collections.Generic.IDictionary generic interface.

  • Each element is a key/value pair for enumeration purposes.

    NoteNote

    The nongeneric SortedList class returns DictionaryEntry objects when enumerated, while the two generic types return KeyValuePair objects.

  • Elements are sorted according to an System.Collections.IComparer implementation (for nongeneric SortedList) or an System.Collections.Generic.IComparer implementation (for the two generic classes).

  • Each class provides properties that return collections containing only the keys or only the values.

The following table lists some of the differences between the two sorted list classes and the SortedDictionary class.

SortedList nongeneric class and SortedList generic class SortedDictionary generic class

The properties that return keys and values are indexed, allowing efficient indexed retrieval.

No indexed retrieval.

Retrieval is O(log n).

Retrieval is O(log n).

Insertion and removal are generally O(n); however, insertion is O(1) for data that are already in sort order, so that each element is added to the end of the list. (This assumes that a resize is not required.)

Insertion and removal are O(log n).

Uses less memory than a SortedDictionary.

Uses more memory than the SortedList nongeneric class and the SortedList generic class.

NoteNote

For values that contain their own keys (for example, employee records that contain an employee ID number), you can create a keyed collection that has some characteristics of a list and some characteristics of a dictionary by deriving from the KeyedCollection generic class.

See Also

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.