asp.net.ph

Hashtable Class

System.Collections Namespace


Represents a collection of key-and-value pairs that are organized based on the hash code of the key.

Hashtable Class Members

Collapse   Constructors

Visibility Constructor Parameters
public Hashtable ( )
public Hashtable ( Int32 capacity )
public Hashtable ( Int32 capacity , Single loadFactor )
public Hashtable ( Int32 capacity , Single loadFactor , IHashCodeProvider hcp , IComparer comparer )
public Hashtable ( Int32 capacity , Single loadFactor , IEqualityComparer equalityComparer )
public Hashtable ( IHashCodeProvider hcp , IComparer comparer )
public Hashtable ( IEqualityComparer equalityComparer )
public Hashtable ( Int32 capacity , IHashCodeProvider hcp , IComparer comparer )
public Hashtable ( Int32 capacity , IEqualityComparer equalityComparer )
public Hashtable ( IDictionary d )
public Hashtable ( IDictionary d , Single loadFactor )
public Hashtable ( IDictionary d , IHashCodeProvider hcp , IComparer comparer )
public Hashtable ( IDictionary d , IEqualityComparer equalityComparer )
public Hashtable ( IDictionary d , Single loadFactor , IHashCodeProvider hcp , IComparer comparer )
public Hashtable ( IDictionary d , Single loadFactor , IEqualityComparer equalityComparer )

Collapse   Properties

Visibility Name Value Type Accessibility
public Count Int32 [ Get ]
public IsFixedSize Boolean [ Get ]
public IsReadOnly Boolean [ Get ]
public IsSynchronized Boolean [ Get ]
public Item ( Object key ) Object [ Get , Set ]
public Keys ICollection [ Get ]
public SyncRoot Object [ Get ]
public Values ICollection [ Get ]

Collapse   Methods

Visibility Name Parameters Return Type
public Add ( Object key , Object value ) Void
public Clear ( ) Void
public Clone ( ) Object
public Contains ( Object key ) Boolean
public ContainsKey ( Object key ) Boolean
public ContainsValue ( Object value ) Boolean
public CopyTo ( Array array , Int32 arrayIndex ) Void
public GetEnumerator ( ) IDictionaryEnumerator
protected GetHash ( Object key ) Int32
public GetObjectData ( SerializationInfo info , StreamingContext context ) Void
protected KeyEquals ( Object item , Object key ) Boolean
public OnDeserialization ( Object sender ) Void
public Remove ( Object key ) Void
public static Synchronized ( Hashtable table ) Hashtable

Each element is a key-and-value pair stored in a DictionaryEntry object. A key cannot be a null reference (Nothing in Visual Basic), but a value can be.

The objects used as keys in a Hashtable must implement or inherit the Object.GetHashCode and Object.Equals methods. If key equality were simply reference equality, the inherited implementation of these methods would suffice. Furthermore, these methods must produce the same results when called with the same parameters while the key exists in the Hashtable. Key objects must be immutable as long as they are used as keys in the Hashtable.

When an element is added to the Hashtable, the element is placed into a bucket based on the hash code of the key. Subsequent lookups of the key use the hash code of the key to search in only one particular bucket, thus substantially reducing the number of key comparisons required to find an element.

The load factor of a Hashtable determines the maximum ratio of elements to buckets. Smaller load factors cause faster average lookup times at the cost of increased memory consumption. The default load factor of 1.0 generally provides the best balance between speed and size. A different load factor can also be specified when the Hashtable is created.

As elements are added to a Hashtable, the actual load factor of the Hashtable increases. When the actual load factor reaches the load factor, the number of buckets in the Hashtable is automatically increased to the smallest prime number that is larger than twice the current number of Hashtable buckets.

Each key object in the Hashtable must provide its own hash function, which can be accessed by calling GetHash. However, any object implementing IHashCodeProvider can be passed to a Hashtable constructor, and that hash function is used for all objects in the table.

The foreach statement of the C# language requires the type of each element in the collection. Since each element of the Hashtable is a key-and-value pair, the element type is not the type of the key or the type of the value. Instead, the element type is DictionaryEntry. For example: foreach (DictionaryEntry myEntry in myHashtable) {...}

Example

The following example shows how to create and initialize a Hashtable and how to print out its keys and values.

using System;
using System.Collections;
public class SamplesHashtable  {

   public static void Main()  {

      // Creates and initializes a new Hashtable.
      Hashtable myHT = new Hashtable();
      myHT.Add ("First", "Hello");
      myHT.Add ("Second", "World");
      myHT.Add ("Third", "!");

      // Displays the properties and values of the Hashtable.
      Response.Write ("myHT" );
      Response.Write ("  Count:    {0}", myHT.Count );
      Response.Write ("  Keys and Values:" );
      PrintKeysAndValues ( myHT );
   }


   public static void PrintKeysAndValues ( Hashtable myList )  {
      IDictionaryEnumerator myEnumerator = myList.GetEnumerator();
      Response.Write ("\t-KEY-\t-VALUE-" );
      while ( myEnumerator.MoveNext() )
         Response.Write ("\t{0}:\t{1}", myEnumerator.Key, myEnumerator.Value);
      Response.Write ();
   }
}
/* 
This code produces the following output.

myHT
  Count:    3
  Keys and Values:
    -KEY-    -VALUE-
    Third:    !
    Second:    World
    First:    Hello
*/
  C# VB JScript
Skip Navigation Links



Home
Suggested Reading


Previous page Back to top Next page

© 2000-2010 Rey Nuñez All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note

You can help support asp.net.ph