eBabel Blog

Make a website

70-536 exam: Hashtable class

11 March 2010

The exam 70-536 requires you are familiar with standard/core library .NET system classes like the Hashtable:

Namespace: System.Collections
Assembly: mscorlib (in mscorlib.dll)

The class implements the collection, enumerable and dictionary interfaces

C#
[SerializableAttribute]
[ComVisibleAttribute(true)]
public class Hashtable : IDictionary, ICollection,
IEnumerable, ISerializable, IDeserializationCallback, ICloneable

As Hashtables implement the interface IDictionary we can use this strutuce for in-memory lookup tables (key/value pairs)

IDictionary(TKey, TValue) Interface represents a generic collection of key/value pairs

As Hashtables also implement the interfaces IEnumerable and ICollection we can iterate through them (for each ... loop):

IEnumerable Interface exposes the enumerator, which supports a simple iteration over a non-generic collection.
ICollection Interface defines size, enumerators, and synchronization methods for all nongeneric collections.

http://msdn.microsoft.com/en-us/library/system.collections.hashtable.aspx

This is a common in-memory data structure in computer sciences. To understand a bit better the generic/background concept better:

In computer science, a hash table or hash map is a data structure that uses a hash function to efficiently map certain identifiers or keys (e.g., person names) to associated values (e.g., their telephone numbers). The hash function is used to transform the key into the index (the hash) of an array element (the slot or bucket) where the corresponding value is to be sought.

Source: http://en.wikipedia.org/wiki/File:Hash_table_3_1_1_0_1_0_0_SP.svg

Ideally the hash function should map each possible key to a different slot index, but this ideal is rarely achievable in practice (unless the hash keys are fixed; i.e. new entries are never added to the table after creation). Most hash table designs assume that hash collisions — pairs of different keys with the same hash values — are normal occurrences and must be accommodated in some way.

Source: http://en.wikipedia.org/wiki/Hashtable

To finish I want to go the through the msdn online documentation, which gives has code samples of how to use Hashtables in .NET.

I am going to highlight a couple of key elements here, lets with creating and populating a sample Hashtable called openWith (matching standard file extensions with software) :

Hashtable openWith = new Hashtable();
openWith.Add("txt", "notepad.exe");
openWith.Add("bmp", "paint.exe");
openWith.Add("dib", "paint.exe");
openWith.Add("rtf", "wordpad.exe");

Next we can iterate through the results (using the IEnumerable and IDictionary interfaces)

foreach( DictionaryEntry de in openWith )
{
Console.WriteLine("Key = {0}, Value = {1}", de.Key, de.Value);
};

The IDictionary interface has a ContainsKey method

if (!openWith.ContainsKey("doc"))
{
Console.WriteLine("Key \"doc\" is not found.");
}

For more details see http://msdn.microsoft.com/en-us/library/system.collections.hashtable.aspx

If you liked this blog post, you may want to subscribe to my news feed in your RSS reader.