Hashtables vs. DictionaryBase

  • Thread starter Thread starter Michael Maercker
  • Start date Start date
M

Michael Maercker

hi! i'm wondering what the differences are between hashtables und
dictionarybases; can both of them store several different types of objects,
each accessible by a string key? currently i'm using a class deriving from a
dictionarybase, because i thought all objects in a hashtable had to be of
the same type; was i wrong? should i switch?

thanks,
mike
 
Hashtable can store any object you like, and so can a dictionaryBase.
Dictionary base is meant to be used as a base class for a custom collection
you would write. This allows you to easily create a strongly typed
collection. DictionaryBase uses an internal hastable for storage so there
performance is equivelent

Hope this clears things up!

Tom Wisnowski
MCP MCAD
 
The only real difference is that DictionaryBase allows you to build a
wrapper around
an hashtable in order to define a strongly typed dictionary.
 
Say you want a dictionary of keys and values of System.Int32.
You could use the System.Collections.Hashtable to do that.
However, Hashtable uses keys and values of System.Object
but not System.Int32.
So, that hashtable would actually accept *any* type
since in .NET, everything is derived from System.Object.

You could then inherit DictionaryBase
and define an Add Method whose signature is
public void Add ( int key, int value )
rather than
public void Add ( object key, object value )

You will then get a dictionary which is "strongly typed" to use System.Int32
rather than System.Object.

You may want to read an article i wrote about strongly typed collections
on DeveloperLand:
http://www.developerland.com/DotNet/General/97.aspx
 
Back
Top