Home > Guides > Programming > .NET and Windows Programming

Toggle Open Guide Table of ContentsGuide Contents

Close Table of ContentsGuide Contents

Close Table of Contents

Hashtable Construction Options

Last updated Dec 5, 2003.

The easiest way to create a Hashtable is by calling the default constructor, as shown in the code sample above. This call creates a Hashtable collection with default options that make the best balance between speed, size, and functionality. However, in special circumstances you might want to change the way that the collection is created. The various overloaded Hashtable constructors let you specify several parameters, as outlined below.

  • Specifying an initial capacity initializes the Hashtable to hold a certain number of items. This saves some time in resizing the collection as items are added. This is very beneficial if you have a general idea how many items will go into the collection.

  • The load factor specifies a ratio between elements and buckets in the Hashtable. Without going into the detail of buckets (see the official documentation for details), a lower number here gives faster performance at the expense of increased memory consumption. The number you supply here can be between 0.1 and 1.0. A value of 1.0 gives the best balance between speed and memory consumption.

  • The default constructor has the Hashtable use the individual keys' GetHash function to obtain hash codes for keys. To override that behavior, you can pass a reference to an object that implements the IHashCodeProvider interface. The Hashtable will then use this interface to obtain hash codes for keys. In collections derived from Hashtable, you can also change the hash code provider by setting the protected hcp property.

  • By default, Hashtable compares keys by calling individual key objects' Equals method. You can override this by passing an IComparer reference to the constructor. In addition, derived objects can change the comparison function by setting the protected comparer property.

The Hashtable is a very powerful collection type that is used in many places throughout the .NET Framework. If you find that you need to store and access objects based on a key, the Hashtable is the first place you should turn. This collection type has other methods and properties that I did not discuss here. You should refer to the official documentation for more details.

Discussions

Copies of the array?
Posted Dec 23, 2008 03:40 PM by luige21
1 Replies
Hi
Posted Dec 5, 2008 05:10 AM by ajay2000bhushan
2 Replies
You have no clue.
Posted Jun 10, 2008 03:28 PM by theinternetmaster
1 Replies

Make a New Comment

You must log in in order to post a comment.

Related Resources

Jim Mischel"Highly unlikely" does not mean "impossible"
By Jim MischelJuly 18, 2009 No Comments

One of my programs crashed the other day in a very unexpected place.  A call to System.Threading.ConcurrentQueue.TryDequeue (from the Parallel Extensions to .NET) resulted in an OverflowException being thrown.  Investigation revealed a pretty serious bug in the System.Random constructor.

It's Here; Put Away Your Pre-Conceptions on What an OS Must Be: Part II
By John TraenkenschuhMay 24, 2009 No Comments

In the last blog in this series, Traenk relates his first experiences with computers and with coding.  But now, some years have passed. . .

It's Here; Put Away Your Pre-Conceptions on What an OS Must Be: Part I
By John TraenkenschuhMay 24, 2009 No Comments

Traenk relates his past experience with Operating Systems that goes back 25 years, ok, more than that but he ain't tellin'

See More Blogs

Informit Network