Author - Dave Amour

Dave Amour has used computers for as long as he can remember and intially started out as an IT trainer delivering a range of IT courses but for the last 11 years has been focusing on the arena of web application development. He was worked for numerous companies over the years and is currently working for Audacs Software Ltd. Dave is also a keen squash player and an active and sucessful member of Experts Exchange

Please feel free to submit any constructive comments which you can do at the bottom of this page.

Dave may be available for programming tuition or consultancy work. Contact via dave@audacs.co.uk
Dave Amour - Click to view CV 

Collection Classes in .net - the HashTable

This is the fourth part of an article on collection classes in .net with C#.  This part covers the HashTable.

For the first part see http://www.audacs.co.uk/ViewPage.aspx?PageID=512

For the next part on the Dictionary & SortedDictionary see http://www.audacs.co.uk/ViewPage.aspx?PageID=516

Ok so a hashtable is a collection class of course and it is found at System.Collections.Hashtable

Each item within a HasTable is actually a struct of the type DictionaryEntry.  A DictionaryEntry struct contains a key and a value.  Thats what HashTables are all about - a collection of things which have a key and a value - eg ISBN Number  + Book Title, Employee Number + Name etc.  The key and value are actually of type object so you can have rich oo objects within your HashTable although realistically you would probably have a simple type as your key.  Each key must be unique though.

So lets have a look at a simple program to work with a HashTable.

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
 
namespace HashTables
{
    class Program
    {
        static void Main(string[] args)
        {
            System.Collections.Hashtable things = new System.Collections.Hashtable();
 
            things.Add(1, "AAA");
            things.Add(2, "BBB");
            things.Add(3, "CCC");
            things.Add(4, "DDD");
            things.Add(5, "EEE");
 
            foreach (DictionaryEntry entry in things)
            {
                Console.WriteLine(entry.Key + ": " + entry.Value);
            }
 
            Console.WriteLine();
 
            foreach (int key in things.Keys)
            {
                Console.WriteLine(key + ": " + things[key]);
            }
 
            Console.Read();
        }
    }
}


I have demonstrated two methods of iterating over the items in a HashTable to give you different options.  In essense though a HashTable is really is that simple.  The beauty of HashTables though lies in what happens under the hood and why they are called a HashTable.

Now I am not going to go into much detail at all in explaining how HashTables work - that's the good thing about encapsulation  - we can code something very complicated and wrap it up in a class and then just use that class and trust that it is doing all the work for us under the hood.  This is true of Hashtable.

However if you are interested in the underlying science and maths then check out http://en.wikipedia.org/wiki/Hashtable

Hashtables are very good at searching and good at working with large amounts of data.    Imagine we had a collection full of objects each with a key - say National Insurance number for the key and then an instance of an Employee class for the value.  In this case then items would exist in the collection in the order they were created and so searching for a particular National insurance number could not use any kind of binary chop and so would just have to trawl through the collection one item at a time until it found the one it needed.

Well what I beleive the HashTable does is that it pushes the key of each item throuhg a hash method.  This method returns a unique number and so when we search for something then the HashTable will push the search key throuh the hash method producing a number and then the item can be found easily as the hash table is stored sorted by this hashed value and so standard binary chop methods can be used.

This is why we often see HashTables have stored things in what looks like apparently random order.

We can emphasise this last point by running the following code:

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
 
namespace HashTables
{
    class Program
    {
        static void Main(string[] args)
        {
            System.Collections.Hashtable things = new System.Collections.Hashtable();
 
            things.Add("AAA", "AAA");
            things.Add("BBB", "BBB");
            things.Add("CCC", "CCC");
            things.Add("DDD", "DDD");
            things.Add("EEE", "EEE");
 
            foreach (DictionaryEntry entry in things)
            {
                Console.WriteLine(entry.Key + ": " + entry.Value);
            }
 
            Console.Read();
        }
    }
}
 


So this time we have used a string as the key and as the value.  When we run this program though we get the following output with the apparently random sorting!

 

Thats about it for the HashTable.

For the next part on the Dictionary & SortedDictionary see http://www.audacs.co.uk/ViewPage.aspx?PageID=516

Leave a comment

Name

Email (optional and not disclosed)



Email address is not disclosed but just used to alert you of new posts if entered
Word CV
HTML CV
PDF CV
Text CV
CMS Lite
ETraining
Planet Health
Florida Health
the Date Shack
Taylors
Emcat
Emtex
Browne Jacobson
Alliance & Leicester
Baird Leisure
Swarfega
Creature Comforts
Rugeley Chess Club
Katnip
Katmaid
Dudley NHS
Contact Me
Current Status