.Net application development specialists
asp.net, c#, vb.net, html, javascript, jquery, html, xhtml, css, oop, design patterns, sql server, mvc and much more
contact: admin@paxium.co.uk

Paxium is the company owned by myself, Dave Amour and used for providing IT contract development services including


  • Application development - Desktop, Web, Services - with Classic ASP, Asp.net WebForms, Asp.net MVC, Asp.net Core
  • Html, Css, JavaScript, jQuery, React, C#, SQL Server, Ado.net, Entity Framework, NHibernate, TDD, WebApi, GIT, IIS
  • Database schema design, implementation & ETL activities
  • Website design and hosting including email hosting
  • Training - typically one to one sessions
  • Reverse Engineering and documentation of undocumented systems
  • Code Reviews
  • Performance Tuning
  • Located in Cannock, Staffordshire
Rugeley Chess Club Buying Butler Cuckooland Katmaid Pet Sitting Services Roland Garros 60 60 Golf cement Technical Conformity Goofy MaggieBears Vacc Track Find Your Smart Phone eBate Taylors Poultry Services Lafarge Rebates System Codemasters Grid Game eBate DOFF

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 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