GOA - System.Collections.Hashtable

Hashtable Class


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

Definition

public class Hashtable: Object, IDictionary, ICloneable

Members Table

MethodDescription
AddAdds an element with the specified key and value into the Hashtable.
ClearRemoves all elements from the Hashtable.
CloneCreates a shallow copy of the Hashtable.
ContainsDetermines whether the Hashtable contains a specific key.
ContainsKeyDetermines whether the Hashtable contains a specific key.
ContainsValueDetermines whether the Hashtable contains a specific value.
CopyToCopies the Hashtable elements to a one-dimensional Array instance at the specified index.
EqualsOverloaded. Compares two objects to determine if they are equal. (Inherited from Object)
GetEnumeratorOverloaded. (Inherited from IEnumerable)
GetHashReturns the hash code for the specified key.
GetHashCodeServes as a hash function for a particular type, suitable for use in hashing algorithms and data structures like a hash table. (Inherited from Object)
GetTypeGets the Type of the current instance. (Inherited from Object)
HashtableOverloaded. Initializes a new instance of Hashtable.
KeyEqualsCompares a specific Object with a specific key in the Hashtable.
ReferenceEqualsDetermines whether the specified Object instances are the same instance. (Inherited from Object)
RemoveRemoves the element with the specified key from the Hashtable.
SynchronizedReturns a synchronized wrapper for the Hashtable.
System.Collections.IEnumerable.GetEnumeratorReturns an IEnumerator that can iterate through the Hashtable.
ToStringReturns a String that represents the current Object. (Inherited from Object)
this[]Gets or sets the value associated with the specified key.
PropertyDescription
CountGets the number of key-and-value pairs contained in the Hashtable.
IsFixedSizeGets a value indicating whether the Hashtable has a fixed size.
IsReadOnlyGets a value indicating whether the Hashtable is read-only.
IsSynchronizedGets a value indicating whether access to the Hashtable is synchronized.
KeysGets an ICollection containing the keys in the Hashtable.
SyncRootGets an object that can be used to synchronize access to the Hashtable.
ValuesGets an ICollection containing the values in the Hashtable.
comparerGets or sets the comparer to use for the Hashtable.
hcpGets or sets the object that can dispense hash codes for the Hashtable.

Inheritance Hierarchy

Object
Hashtable

Examples

// This examples shows how to create and initialize a Hashtable.

// It shows also some properties and method of the Hashtable class.

 

using System;

using System.Diagnostics;

using System.Collections;

 

public class SampleHashtable

{

     static void Main()

     {

          // Creates and initializes a new Hashtable.

          Hashtable myHT = new Hashtable();

 

          // Adds elements to the Hashtable.

          myHT.Add("First" , "Hello");

          myHT.Add("Second", "World");

          myHT.Add("Third", "!");

          myHT.Add("Fourth", "Hi");

          myHT.Add("Fifth", "Bye");

 

          // Displays the properties and values of the Hashtable.

          Debug.WriteLine("\nProperties of the collection:");

 

          bool FS = myHT.IsFixedSize;

          Debug.WriteLine("\tFixed size:"+FS.ToString());

 

          bool RO = myHT.IsReadOnly;

          Debug.WriteLine("\tRead only:"+RO.ToString());

 

          // Displays the contents of the Hashtable.

          Debug.WriteLine("\nInitial contents of the Hashtable:");

          DisplayValues(myHT);

 

          // Searches the Hashtable with Contains.

          bool t =myHT.Contains("Third");

          Debug.WriteLine("\n"+@"Contains ""Third""(using the Contains method):"+t.ToString());

 

          bool t1 =myHT.ContainsKey("First");

          Debug.WriteLine("\n"+@"Contains ""First""(using the ContainsKey method):"+t1.ToString());

 

          bool t2 =myHT.ContainsValue("Bye");

          Debug.WriteLine("\n"+@"Contains ""Bye""(using the ContainsValue method):"+t2.ToString());

 

          // Creates and displays the contents of an array of string.

          string[] myStrArray = new string[5] { "abc", "def", "ghi", "jkl", "mno" };

          Debug.Write("\nContents of an array of string:");

          DisplayArrayValues(myStrArray);

 

          // Copies the keys of the Hashtable to the array of string.

          myHT.Keys.CopyTo(myStrArray,0);

          Debug.Write("\n\nContents of the array after copying the keys of the collection in it:");

          DisplayArrayValues(myStrArray);

 

          // Removes an element from the Hashtable.

          myHT.Remove(4);

          Debug.WriteLine("\n\n"+@"Contents of the collection after removing the key ""4"":");

          DisplayValues2(myHT);

     }

 

     // Displays values using Keys property and the this[] method.

     public static void DisplayValues( Hashtable myHashtable )

     {

          ICollection myKeys = myHashtable.Keys;

          foreach ( object k in myKeys )

               Debug.WriteLine("\t"+k+":"+myHashtable[k].ToString());

     }

 

     // Displays values using an enumerator.

     public static void DisplayValues2( Hashtable myHashtable )

     {

          DictionaryEntry myDE;

          System.Collections.IEnumerator myEnumerator = myHashtable.GetEnumerator();

          while ( myEnumerator.MoveNext() )

               if ( myEnumerator.Current != null )

                    myDE = (DictionaryEntry) myEnumerator.Current;

                    Debug.WriteLine("\t"+myDE.Key.ToString()+":"+myDE.Value.ToString());

     }

 

     // Displays values using the foreach statement.

     public static void DisplayValues3 ( Hashtable myHashtable )

     {

          foreach ( DictionaryEntry myDE in myHashtable )

               Debug.WriteLine("\t"+myDE.Key.ToString()+":"+myDE.Value.ToString());

     }

 

     // Displays the values of an array.

     public static void DisplayArrayValues ( Array array )

     {

          for ( int i = 0; i < array.Length; i++ )

          {

               Debug.Write(array.GetValue(i)+" ");

          }

     }

}

 

 

/*

 

The output of the sample.

 

Properties of the collection:

     Fixed size:False

     Read only:False

 

Initial contents of the Hashtable:

     Fifth:Bye

     First:Hello

     Second:World

     Fourth:Hi

     Third:!

 

Contains "Third"(using the Contains method):True

Contains "First"(using the ContainsKey method):True

Contains "Bye"(using the ContainsValue method):True

 

Contents of an array of string:abc def ghi jkl mno

Contents of the array after copying the keys of the collection in it:Third Fourth Second First Fifth

 

Contents of the collection after removing the key "4":

     Fifth:Bye

     First:Hello

     Second:World

     Fourth:Hi

     Third:!

 

*/

Class Information

NamespaceSystem.Collections
Flash Librarycorlib.scl
Flash Library Version2.0.0.2466
Silverlight LibrarySystem.Windows.Forms.dll



© 2003-2007 NETiKA Technologies. All rights reserved.