Represents a collection of key-and-value pairs that are organized based on the hash code of the key.
public class Hashtable: Object, IDictionary, ICloneable
ObjectHashtable
// 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.
// Displays the properties and values of the Hashtable.
Debug.WriteLine("\nProperties of the collection:");
Debug.WriteLine("\tFixed size:"+FS
.ToString());
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.
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:!
*/
Namespace System.Collections Flash Library corlib.scl Flash Library Version 2.0.0.2466 Silverlight Library System.Windows.Forms.dll
| © 2003-2007 NETiKA Technologies. All rights reserved. |