Category Archives: Uncategorized

Dictionaries in C#: The SortedList

There are times when you need to sort your collection easily. Fortunately, there is a special dictionary called the SortedList to handle these needs. The SortedList is part of the System.Collections.Specialized library (don’t forget your using reference!).

There is one thing to understand that is a bit counter intuitive. The SortedList sorts off of the Key, and not the Value. While this may not seem natural, it can work to your advantage in times when users want to have peculiar sort orders. You can store the odd sort in the key, then display the values to the users.

In my simple example below, I’ve created a SortedList of the artists I might listen to during a day of programming. To make the sort case insensitive, I entered all the keys in lower case, then the value is the artist name in normal type.

      SortedList myMusic = new SortedList();

 

      myMusic.Add(“zztop”, “ZZTop”);

      myMusic.Add(“midnight synidicate”, “Midnight Syndicate”);

      myMusic.Add(“kate bush”, “Kate Bush”);

      myMusic.Add(“bond”, “Bond”);

      myMusic.Add(“clint black”, “Clint Black”);

      myMusic.Add(“queen latifah”, “Queen Latifah”);

 

      foreach (DictionaryEntry favoriteArtist in myMusic)

      {

        Console.WriteLine(favoriteArtist.Value);

      }

 

Produces this output in the command window:

Bond
Clint Black
Kate Bush
Midnight Syndicate
Queen Latifah
ZZTop

When you need to maintain a frequently changing list of values that need to be sorted, using the SortedList dictionary can be a real time saver.

Dictionaries in C#: The ListDictionary

Hashtables (see my posts from the last two days ago) are a great, general use type of dictionary, but they have the one drawback of requiring some extra overhead to implement. For very small collections, say 10 items or less, the overhead can have an impact on performance. To solve this, Microsoft has created an alternate form of hashtable called a ListDictionary.

Behind the scenes, ListDictionaries are implemented as simple arrays. For small collections then their performance is very fast. They have the same interface as a hashtable, so it’s very easy to swap from one to another, aside of course from the issue of having to recompile your project.

Before starting, in addition to the using System.Collections reference, you will also need to include a using System.Collections.Specialized reference to the top of your code.

Next, all you have to do is create a variable of type ListDictionary. After that, using it is identical to using a HashTable.

      ListDictionary genders = new ListDictionary();

 

      genders.Add(“M”, “Male”);

      genders.Add(“F”, “Female”);

 

Frequently when dealing with collections the size of the collection is quite small. Lists of values for drop down boxes, which hold data for things like employee type, gender, ethnicity, and perhaps ranges of ages are all times when you will have a short list. For those times, when you are certain the list is small, use a ListDictionary to increase the performance of your application.