Represents a collection of key-and-value pairs that are organized based on the hash code of the key.Example
[Visual Basic, C#, JScript] The following example shows how to create and initialize a Hashtable and how to print out its keys and values.[Visual Basic]
Imports System
Imports System.Collections
Imports Microsoft.VisualBasicPublic Class SamplesHashtable
Public Shared Sub Main()
' Creates and initializes a new Hashtable.
Dim myHT As New Hashtable()
myHT.Add("First", "Hello")
myHT.Add("Second", "World")
myHT.Add("Third", "!")
' Displays the properties and values of the Hashtable.
Console.WriteLine("myHT")
Console.WriteLine(" Count: {0}", myHT.Count)
Console.WriteLine(" Keys and Values:")
PrintKeysAndValues(myHT)
End Sub
Public Shared Sub PrintKeysAndValues(myList As Hashtable)
Dim myEnumerator As IDictionaryEnumerator = myList.GetEnumerator()
Console.WriteLine(ControlChars.Tab + "-KEY-" + ControlChars.Tab _
+ "-VALUE-")
While myEnumerator.MoveNext()
Console.WriteLine(ControlChars.Tab + "{0}:" + ControlChars.Tab _
+ "{1}", myEnumerator.Key, myEnumerator.Value)
End While
Console.WriteLine()
End Sub
End Class' This code produces the following output.
'
' myHT
' Count: 3
' Keys and Values:
' -KEY- -VALUE-
' Third: !
' Second: World
' First: Hello
[C#]
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.
Console.WriteLine( "myHT" );
Console.WriteLine( " Count: {0}", myHT.Count );
Console.WriteLine( " Keys and Values:" );
PrintKeysAndValues( myHT );
}
public static void PrintKeysAndValues( Hashtable myList ) {
IDictionaryEnumerator myEnumerator = myList.GetEnumerator();
Console.WriteLine( "\t-KEY-\t-VALUE-" );
while ( myEnumerator.MoveNext() )
Console.WriteLine("\t{0}:\t{1}", myEnumerator.Key, myEnumerator.Value);
Console.WriteLine();
}
}
/*
This code produces the following output.myHT
Count: 3
Keys and Values:
-KEY- -VALUE-
Third: !
Second: World
First: Hello
*/
[JScript]
import System
import System.Collections// Creates and initializes a new Hashtable.
var myHT : Hashtable = new Hashtable()
myHT.Add("First", "Hello")
myHT.Add("Second", "World")
myHT.Add("Third", "!")// Displays the properties and values of the Hashtable.
Console.WriteLine("myHT")
Console.WriteLine(" Count: {0}", myHT.Count)
Console.WriteLine(" Keys and Values:")
PrintKeysAndValues(myHT)
function PrintKeysAndValues(myList : Hashtable){
var myEnumerator : IDictionaryEnumerator = myList.GetEnumerator()
Console.WriteLine("\t-KEY-\t-VALUE-")
while(myEnumerator.MoveNext())
Console.WriteLine("\t{0}:\t{1}", myEnumerator.Key, myEnumerator.Value)
Console.WriteLine()
}// This code produces the following output.
//
// myHT
// Count: 3
// Keys and Values:
// -KEY- -VALUE-
// Third: !
// Second: World
// First: Hello From MSDN
[Visual Basic, C#, JScript] The following example shows how to create and initialize a Hashtable and how to print out its keys and values.[Visual Basic]
Imports System
Imports System.Collections
Imports Microsoft.VisualBasicPublic Class SamplesHashtable
Public Shared Sub Main()
' Creates and initializes a new Hashtable.
Dim myHT As New Hashtable()
myHT.Add("First", "Hello")
myHT.Add("Second", "World")
myHT.Add("Third", "!")
' Displays the properties and values of the Hashtable.
Console.WriteLine("myHT")
Console.WriteLine(" Count: {0}", myHT.Count)
Console.WriteLine(" Keys and Values:")
PrintKeysAndValues(myHT)
End Sub
Public Shared Sub PrintKeysAndValues(myList As Hashtable)
Dim myEnumerator As IDictionaryEnumerator = myList.GetEnumerator()
Console.WriteLine(ControlChars.Tab + "-KEY-" + ControlChars.Tab _
+ "-VALUE-")
While myEnumerator.MoveNext()
Console.WriteLine(ControlChars.Tab + "{0}:" + ControlChars.Tab _
+ "{1}", myEnumerator.Key, myEnumerator.Value)
End While
Console.WriteLine()
End Sub
End Class' This code produces the following output.
'
' myHT
' Count: 3
' Keys and Values:
' -KEY- -VALUE-
' Third: !
' Second: World
' First: Hello
[C#]
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.
Console.WriteLine( "myHT" );
Console.WriteLine( " Count: {0}", myHT.Count );
Console.WriteLine( " Keys and Values:" );
PrintKeysAndValues( myHT );
}
public static void PrintKeysAndValues( Hashtable myList ) {
IDictionaryEnumerator myEnumerator = myList.GetEnumerator();
Console.WriteLine( "\t-KEY-\t-VALUE-" );
while ( myEnumerator.MoveNext() )
Console.WriteLine("\t{0}:\t{1}", myEnumerator.Key, myEnumerator.Value);
Console.WriteLine();
}
}
/*
This code produces the following output.myHT
Count: 3
Keys and Values:
-KEY- -VALUE-
Third: !
Second: World
First: Hello
*/
[JScript]
import System
import System.Collections// Creates and initializes a new Hashtable.
var myHT : Hashtable = new Hashtable()
myHT.Add("First", "Hello")
myHT.Add("Second", "World")
myHT.Add("Third", "!")// Displays the properties and values of the Hashtable.
Console.WriteLine("myHT")
Console.WriteLine(" Count: {0}", myHT.Count)
Console.WriteLine(" Keys and Values:")
PrintKeysAndValues(myHT)
function PrintKeysAndValues(myList : Hashtable){
var myEnumerator : IDictionaryEnumerator = myList.GetEnumerator()
Console.WriteLine("\t-KEY-\t-VALUE-")
while(myEnumerator.MoveNext())
Console.WriteLine("\t{0}:\t{1}", myEnumerator.Key, myEnumerator.Value)
Console.WriteLine()
}// This code produces the following output.
//
// myHT
// Count: 3
// Keys and Values:
// -KEY- -VALUE-
// Third: !
// Second: World
// First: Hello From MSDN
通过KEY方便地查询对应的值。