Dictionary versus HashTables

So I learned something very interesting today on SO from this link: Dictionary over Hashtables. I was under the impression at one point that a Hash tables was a Dictionary and that a Hashtable was better and faster. I was mistaken. I know, this doesn’t happen often.

First a HashTable is a Dictionary type. For those of you that don’t know, when one says that, basically a HashTable object inherits its core features from the Dictionary object. A Dictionary is also faster than a Hashtable and safer to use. Why you say?

Well the Dictionary is a type safe object. So what does type safe mean? Well, type safe means that when you create a reference object that instantiates that object, it requires implicit knowledge of what type of object you are going to pump into it. For instance, in the case of a dictionary, you could do something like this:

 

var items = new Dictionary<string, int>();             items.Add("1", 1);             items.Add("2", 2);             items.Add("3", 3);             items.Add("4", 4);             items.Add("5", 5);             items.Add("6", 6);             items.Add("7", 7);             items.Add("8", 8);             items.Add("9", 9);             items.Add("10", 10);             items.Add("11", 11);             return new SelectList(items);

 

Now as you can see on the initialization of the dictionary, we are specifying two parameters, string and int. The first parameter states the type the key will be and the second defines what the value will be. With this declaration, you are giving the compiler a guarentee that this object will ONLY use those types to define the key and value. This causes 2 things.

First it makes for less development errors because no one can sneak values of different types in without causing a compiler error. If the Dictionary is not type safe, someone could put a POCO object or a primitive type other than string and int. This wouldn’t cause a compiler error if it isn’t type safe, but if you try to access the object without a cast, a runtime error would occur. This creates bugs where they shouldn’t be.

Second it provides better performance due to the lack of boxing and unboxing (casting).

HashTables

HashTables are to Dictionary like ArrayList is it’s generic counter part List<T>. Lets look at a HasTable example:

  Hashtable hashtable = new Hashtable();             hashtable.Add("Integer", 1);             hashtable.Add("Double", 55.6);             hashtable.Add("String", “What up”);

Now you will notice that I have this example set up that it shows 3 different types, int, double and string. The Hash is always string in this case. Now take this example of an attempt to use this variable.

int value = (int)hashtable["String"];

 

Now this is obviously a very simple example, but in this example, the code would blow up because you are assuming an integer because of the value of the first key value pair (“Integer”). This is what happens when you aren’t type safe and another developer might want to use your library. If you are the only one using it, you still might forget and cause some mayhem.

 

Hope this helps other with some clarification!

 

Happy coding! Smile

Advertisements

About Gregg Coleman

I am Senior-level Software Engineer working primarily these days with .NET. I have a good working knowledge of ASP.NET MVC, Web Forms, WCF web services and Windows Services. I spend much of my time in the Web Services (SOAP and REST) world in my current job designing and implementing various SOA architectures. I have been in the software engineering industry for about 6 years now and will not now nor ever consider myself an "expert" in programming because there is always so much to learn. My favorite thing about designing software is there are always new emerging technologies and something to learn every day! My current job has me spending much of my job on the bleeding edge of technologies and changing gears all the time, so I'm never bored and always challenged. On my spare time I enjoy weight training, reading and venturing to new places near by. Of course programing and learning new technologies are another hobby of mine.
This entry was posted in .NET, .NET 4.5, C#, Generics and tagged , , , , , . Bookmark the permalink.

2 Responses to Dictionary versus HashTables

  1. travel says:

    I’ll right away clutch your rss as I can’t in finding your email subscription hyperlink or e-newsletter
    service. Do you’ve any? Kindly permit me recognize in order that I could subscribe. Thanks.

    • @travel – sorry about that. I kept forgetting to add the widgets. they have been added and you can now subscribe via email and RSS feed. Let me know if you have any problems. This is my first time using the word press widgets.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s