Contents
Download source [^]
Introduction
System.Collections.Hashtable is widely used for storing arrays accessed by keys
rather than indices like the System.Collections.ArrayList. As of today, the System.Collections.Hashtable
provides a means of storing keys and values of instances of System.Object.The Hashtable
is really useful, however in .NET there is a drawback when using the Hashtable,
and actually any type of built-in collection type; the lack of Generics, which is
the possibillity to easily define strongly typed collections.
Top 
The problem
When a user adds a System.Int32(or any other type) to a System.Collections.Hashtable,
there is an implicit cast to System.Object.Similarly, if a System.Int32 is retrieved
from the list, it must be cast at run time from a System.Object.This lack of type
safety at compile time is both tedious for the developer and prone to error.
Although there is a standard approach for defining a strongly typed dictionary by
inheriting the abstract base class of System.Collections.DictionaryBase and implement
type safe members as wished, the inner Hashtable provided by DictionaryBase still
uses System.Object, so implicit and explicit casts cannot be avoided when using
that approach.If type checking is done at compile time rather than at run time,
then performance improves.
Top 
The solution
Lately, I have been in need for a strongly typed dictionary using slots of System.Int32
rather than System.Object.
Therefore, I wrote a class to provide me with that functionality. Performance tests
shows that this is not only a System.Int32 type safe dictionary, but it is also
much faster than System.Collections.Hashtable.
Although the class uses slots of System.Int32, it could be modified in order to
define slots of other types as well.
.NET 2.0 will introduce Generics. I can't wait!Top 