1 using System; 2 using System.Collections; 3 4 public class SamplesHashtable 5 { 6 public static void Main() 7 { 8 Hashtable myHT = new Hashtable(); 9 10 myHT.Add("Ton V. Bergyk", "023-010-66756"); 11 myHT.Add("Tom Sony", "086-010-27654"); 12 myHT.Add("Mr. John", "071-222-33445"); 13 14 myHT["Mr. John"] = "071-222-33445"; //加入或修改 15 16 PrintKeysAndValues( myHT ); 17 PrintByKeys( myHT ); 18 } 19 20 public static void PrintKeysAndValues( Hashtable myList ) 21 { 22 IDictionaryEnumerator myEnumerator = myList.GetEnumerator(); 23 while ( myEnumerator.MoveNext() ) 24 Console.WriteLine(" {0}: {1}", 25 myEnumerator.Key, myEnumerator.Value); 26 Console.WriteLine(); 27 } 28 29 public static void PrintByKeys( Hashtable myList ) 30 { 31 IEnumerator ie = myList.Keys.GetEnumerator(); 32 while( ie.MoveNext() ) 33 { 34 object key = ie.Current; 35 object value = myList[ key ]; 36 37 Console.WriteLine(" {0}: {1}", key, value ); 38 } 39 Console.WriteLine(); 40 41 foreach( string key in myList.Keys ) 42 { 43 Console.WriteLine(" {0}: {1}", key, myList[key] ); 44 } 45 } 46 }