Dictionaries in C#: The HybridDictionary

So far we’ve looked at the HashTable collection, and determined that it’s the way to go for large collections. We’ve also taken a look at the ListDictionary, which is geared towards very small collections. But what if you don’t know how big your collection will be?

There are many situations where at design time you simply can not know what the final size of your collection will be. Sometimes it will depend on user input, what range of data the user selects for example. Other times it may be dependant on the data, how much is out there for you to use. There’s enough uncertainty in all our lives to get stressed out, so to keep us calm Microsoft has provided the HybridDictionary collection.

A HybridDictionary will self monitor itself. Initially, and while it remains small the HybridDictionary will internally keep itself as a ListDictionary. Once it exceeds a certain threshold, it will then convert it’s data into a HashTable. The nice part is you don’t have to care when, or what state it is currently in. All you have to know is the HybridDictionary is managing the memory most efficiently on your behalf.

Like it’s sibling the ListDictionary, the HybridDictionary will require a using System.Collections.Specialized reference. After that, it has the same interface as the HashTable and can be treated as such.

      HybridDictionary genders = new HybridDictionary();

 

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

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

 

The question many will ask “well why not just use a HybridDictionary all the time?” As with most things in life, there is a trade off. There is a some overhead associated with HybridDictionaries, as they have to constantly monitor their own size, then convert back and forth between ListDictionaries and HashTables as needed. In return, you get the best possible performance from your collection as it changes in size.

The moral of the story, when you know you have a small collection, use a ListDictionary. On the other hand, if you know with certainty your collection will be large, use a HashTable. But for all those other times when you just don’t know, the HybridDictionary is your answer.

Advertisements

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.

The Hashtable Demystified

After reading yesterday’s post, one of my coworkers asked me “so what exactly is a hash and how does it work”. Fair question, in my post I concentrated more on how to use the dictionary than what it was, so it does deserve a little explanation.

When you enter a Key as part of the Key/Value pair, you use something human readable like “ArcaneCode”. While good for people, it’s very inefficient for the computer. Thus the .Net Framework runs your key through a sophisticated algorithm called a “hash”.

The output of this hash algorithm is a number, and that number is what the framework uses for the true key to your data. Thus “ArcaneCode” might produce a hash of 1234. It’s very fast for the computer to find your data when it can compare numbers, hence the usefulness of a hash.

The pseudo code below shows the general process a hash table goes through when retrieving your data.

  1. Key is passed in.
  2. Key is converted to a hash.
  3. The collection is searched for a matching hash.
  4. If found, the value in the dictionary associated with the hashed key is returned.

Hashes are unique, no two strings will ever yield the same hash value. Additionally, they are case sensitive. “ArcaneCode”, “ARCANECODE”, “arcanecode” and “aRCANEcODE” all produce different hash values.

The concept of hash tables are not unique to the .Net Framework, many languages have them, it’s just .Net hides the ugliness and makes them easy to use.

Dictionaries in C#: The Hashtable

About a week ago I began talking about collections in C#, and discussed two special cases, the Stack and the Queue. Another group of specialized collection classes are dictionaries. Dictionaries operate off of a key / value system, where you lookup your information, your value, based on a key you create. The key can be a string, number or other type of data, but it must be unique within the dictionary. Just like in your traditional printed dictionary, you can’t have two entries for the same key.

The most basic type of Dictionary is the Hashtable. The hashtable has the basic functionality for putting entries into the dictionary collection, getting a value based on a key, looping through them, and updating or removing them.

Getting data into the hashtable is pretty easy. First make sure to include a using reference to System.Collections, then simply create a hashtable variable, and finally call the Add method, passing in the key and value.  

      Hashtable myHash = new Hashtable();

 

      myHash.Add(“ArcaneCode”, “arcanecode.wordpress.com”);

 

To read or update the data stored in the value, you can treat the hashtable as if it were a simple array, using the key where you’d normally put the index.

 

      Console.WriteLine(myHash[“ArcaneCode”]);

      myHash[“ArcaneCode”] = “Here I’ve changed the value.”;

      Console.WriteLine(myHash[“ArcaneCode”]);

 

      Console.Read();

 

Hashtables are great for storing data where you have a good primary key you can quickly reference things by. In our example, I’ve stored strings as the value part of the key / value pair, but you can use any type of object for the value, including classes you create.

The examples so far assume you will always want to retrieve a single item out of your hashtable. However, we all know there are times when you will want to iterate over the entire collection. To do this, it’s necessary to understand how your key / value pair is storied.

In keeping with the spirit of OOP, what is actually stored in the hashtable collection is another object of type DictionaryEntry. The dictionary entry object has two essential properties, the Key and the Value, both of which are the generic object type. Here’s an example of how to loop over the myHash collection.

      // Clear out the previous items

      myHash.Clear();

 

      // Now add some new items.

      // Note we use an int as the key this time.

      myHash.Add(1, “Item one”);

      myHash.Add(2, “Item two”);

      myHash.Add(3, “Item three”);

 

      // Loop over the list, writing out the value

      foreach (DictionaryEntry myEntry in myHash)

      {

        Console.WriteLine(myEntry.Value);

      }

 

      Console.Read(); // Wait for user to press a key

 

Note we cycle through a list of DictionaryEntry objects, and each time through get the Value property. If we wanted to get the key, it would be a simple matter of replacing the Value property call with a call to Key.

If you are in a situation where you are not sure of the data types being used in the keys or the values, you should append a .ToString() after a reference to the Key or Value so as not to generate run time errors when displaying. In the above example, we’d have needed to use Console.WriteLine(myEntry.Key.ToString()); since our keys were integers (1, 2, and 3).

As you can see, the hashtable makes it easy to look up values when you have a specific key, yet at the same time is still flexible enough to allow you to iterate over your entire collection when you need to.

Arcane Searches

I thought it was time for another round of Arcane Searches. In case you missed the last round, these are some of the search terms users entered into their search engine and managed to run across my blog. I’ll try to pick out a few that seem to occur frequently and answer them. Let’s get started.

ssis package not reading environment variables

There is a policy setting on the server that dictates whether or not the SQL Server job engine can access environment variables.

virtual pc exit fullscreen

To exit full screen mode, use the Right ALT+ENTER combination. Note that the left ALT key won’t work, you must use the right ALT key, with the Enter key.

connectstring SQL compact, connectionstring sql compact edition

These two show up frequently, the connection string for SQL Server Compact Edition (SSCE) is:

Datasource=”C:\MyData\myFile.sdf”; Password=’mypassword’

Note a few things, first the password part is optional. Second, the password must be enclosed in single quotes. Finally, the path to your sdf data file must be in double quotes.

wrap a string in double quotes c#

I’m guessing the search here is how to embed a double quote in a string, that’s easy. Just use a backslash in front of the double quote, and C# will interpret it as a literal character and not a double quote.

string myvar = “Right before he became unconscious, he said \”Yes dear, those pants to make you look fat.\”. “

virtual pc 2007 different ip addresses

Each Virtual PC, be it 2004 or 2007, appears as it’s own PC to the network. Your network hub can’t tell the difference between a virtual machine and a real one, and assigns each machine it’s IP address.

why would I use an interface C#

Why not? OK, sorry, couldn’t resist. Short answer, an interface gives you a way to treat many kinds of objects as if they were all the same kind of object. For example, if you had manager, field worker, office worker, and executive objects, you could have all of the implement the employee interface, then you could treat them all like generic employees to do things like pay, give vacation, etc.

For a more detailed instruction, see my series of posts which begin at https://arcanecode.wordpress.com/2007/02/13/more-oop-interfaces-in-c-part-1/ or http://shrinkster.com/mjk.

There you go, a quick selection of the most common searches I found, that were not already answered in my previous blog entries.

Arcane Thoughts: Go Wildcats

By now many of you are aware of the devastating tornadoes to hit the state of Alabama, mostly in the Enterprise area. I reside in Alabama, currently near Birmingham but Enterprise is my home town. I graduated from Enterprise High, and my niece is currently a senior there, and was in the school today when the tornado hit. She got hit by some debris when the ceiling partially collapsed, but wasn’t injured thank goodness.

Somewhere between 8 and 15 others weren’t so lucky, and lost their lives (as I write this there are conflicting numbers and it’s unknown if they were students or not). What you can learn from this though, is to be prepared. Weather radios are not expensive, every home should have one (I do!). They will alert you to a variety of severe weather conditions, especially during the night when you might be asleep or not listening to the radio / TV.

Have a designated spot in the house to go when severe weather strikes. For us it’s a spot in our basement. We all know it, and are prepared for it. If you have kids, grab their bicycle helmets and put them on them. Our local meteorologist James Spann mentioned that a good number of the kids who are killed in storm events suffer head trauma, and the helmet could save their lives.

What can you do now? Well those folks in Enterprise are going to need a lot of help. If you can, donate blood, I understand there is a shortage now. Money is good too, and can help with the rebuilding.  This is going to be a tough time, but I know that as time goes buy they will rebuild and bounce back. Enterprise High, Home of the Wildcats.

Go Wildcats!