// http://svc.luckstar.com.tw/CodeHelper/cs/index.html // 2017-03-25, honda@luckstar.com.tw, Create. using System; using System.Collections.Generic; using System.Linq; using System.Text; // add using System.Collections.Specialized; using System.Collections; namespace ConsoleBase { public class CHashtable { Hashtable mList1 = new Hashtable(); public void Run() { Console.WriteLine("Basic:"); // Add some elements to the hash table. There are no // duplicate keys, but some of the values are duplicates. mList1.Add("txt", "notepad.exe"); mList1.Add("bmp", "paint.exe"); mList1.Add("dib", "paint.exe"); mList1.Add("rtf", "wordpad.exe"); // The Add method throws an exception if the new key is // already in the hash table. try { mList1.Add("txt", "winword.exe"); } catch { Console.WriteLine("An element with Key = \"txt\" already exists."); } // The Item property is the default property, so you // can omit its name when accessing elements. Console.WriteLine("For key = \"rtf\", value = {0}.", mList1["rtf"]); // The default Item property can be used to change the value // associated with a key. mList1["rtf"] = "winword.exe"; Console.WriteLine("For key = \"rtf\", value = {0}.", mList1["rtf"]); // If a key does not exist, setting the default Item property // for that key adds a new key/value pair. mList1["doc"] = "winword.exe"; // ContainsKey can be used to test keys before inserting // them. if (!mList1.ContainsKey("ht")) { mList1.Add("ht", "hypertrm.exe"); Console.WriteLine("Value added for key = \"ht\": {0}", mList1["ht"]); } // When you use foreach to enumerate hash table elements, // the elements are retrieved as KeyValuePair objects. Console.WriteLine(); foreach (DictionaryEntry de in mList1) { Console.WriteLine("Key = {0}, Value = {1}", de.Key, de.Value); } // To get the values alone, use the Values property. ICollection valueColl = mList1.Values; // The elements of the ValueCollection are strongly typed // with the type that was specified for hash table values. Console.WriteLine(); foreach (string s in valueColl) { Console.WriteLine("Value = {0}", s); } // To get the keys alone, use the Keys property. ICollection keyColl = mList1.Keys; // The elements of the KeyCollection are strongly typed // with the type that was specified for hash table keys. Console.WriteLine(); foreach (string s in keyColl) { Console.WriteLine("Key = {0}", s); } // Use the Remove method to remove a key/value pair. Console.WriteLine("\nRemove(\"doc\")"); mList1.Remove("doc"); if (!mList1.ContainsKey("doc")) { Console.WriteLine("Key \"doc\" is not found."); } } private void PrintDashLine() { Console.WriteLine(); Console.WriteLine("----------"); } } } result: Basic: An element with Key = "txt" already exists. For key = "rtf", value = wordpad.exe. For key = "rtf", value = winword.exe. Value added for key = "ht": hypertrm.exe Key = dib, Value = paint.exe Key = txt, Value = notepad.exe Key = rtf, Value = winword.exe Key = ht, Value = hypertrm.exe Key = bmp, Value = paint.exe Key = doc, Value = winword.exe Value = paint.exe Value = notepad.exe Value = winword.exe Value = hypertrm.exe Value = paint.exe Value = winword.exe Key = dib Key = txt Key = rtf Key = ht Key = bmp Key = doc Remove("doc") Key "doc" is not found.