New collections in System.Collections

October 3rd, 2005

In this Krzysztof Cwalina post, he mentions the changes to System.Collections in .NET framework 2.0 (yes, 2.0, not 3.0, therefore very prosaic and boring). Of course we know about all that new generics stuff which allows things like List<T>, Collection<T> and Dictionary<TKey,TValue>, which are great things, but further down the list he mentions a new SortedDictionary type:

SortedDictionary<TKey,TValue> does not have a corresponding non-generic collection. It’s similar to SortedList and SortedList<TKey,TValue>, but it’s implemented as a balanced tree (red-black tree). Insertions are on average much faster, but some lookups are slightly slower and memory consumption is larger.

Pretty interesting in light of Luke’s recent aside in the wednesday sessions about the trade-offs of using SortedList versus Hashtable.

One Response to “New collections in System.Collections”

  1. Damien Morton Says:

    Keep in mind that SortedDictionary and SortedList have very different performance and memory characteristics

    SortedDictionary has O(logn) insert/delete/find operations
    SortedList has O(logn) find, and O(n) insert/delete*
    Dictionary has O(1) insert/delete/find

    * these O(n) operations are array copies, and are very fast for small arrays

    SortedDictionary has about 28-32 bytes overhead for each entry
    SortedList has 8 bytes overhead for each entry
    Dictionary has about 16-20 bytes overhead for each entry