Using system; using system. collections. generic; public class example {public static void main () {// create a new dictionary of strings, with string keys. // dictionary <string, string> openwith = new dictionary <string, string> (); // Add some elements to the dictionary. there are no // duplicate keys, but some of the values are duplicates. openwith. add ("TXT", "notepad.exe"); openwith. add ("BMP", "paint.exe"); OP Enwith. add ("Dib", "paint.exe"); openwith. add ("rtf", "wordpad.exe"); // The add method throws an exception if the new key is // already in the dictionary. try {openwith. add ("TXT", "winword.exe");} catch (argumentexception) {console. writeline ("an element with key = \" TXT \ "already exists. ");} // The item property is another name for the indexer, So you // can omit its name when accessing elements. console. Writeline ("for key = \" RTF \ ", value = {0 }. ", openwith [" rtf "]); // The indexer can be used to change the value associated // with a key. openwith ["rtf"] = "winword.exe"; console. writeline ("for key = \" RTF \ ", value = {0 }. ", openwith [" rtf "]); // If a key does not exist, setting the indexer for that key // adds a new key/value pair. openwith ["Doc"] = "winword.exe"; // The indexer throws an exception if the requ Ested key is // not in the dictionary. try {console. writeline ("for key = \" TIF \ ", value = {0 }. ", openwith [" TIF "]);} catch (keynotfoundexception) {console. writeline ("Key = \" TIF \ "is not found. ");} // when a program often has to try keys that turn out not to // be in the dictionary, trygetvalue can be a more efficient // way to retrieve values. string value = ""; if (openwith. trygetvalue ("TIF", out value) {C Onsole. writeline ("for key = \" TIF \ ", value = {0 }. ", value);} else {console. writeline ("Key = \" TIF \ "is not found. ");} // containskey can be used to test keys before inserting // them. if (! Openwith. containskey ("ht") {openwith. add ("ht", "hypertrm.exe"); console. writeline ("value added for key = \" HT \ ": {0}", openwith ["ht"]);} // when you use foreach to enumerate dictionary elements, // The elements are retrieved as keyvaluepair objects. console. writeline (); foreach (keyvaluepair <string, string> kVp in openwith) {console. writeline ("Key = {0}, value = {1}", kVp. key, kVp. value);} // to get the VA Lues alone, use the values property. dictionary <string, string>. valuecollection valuecoll = openwith. values; // The elements of the valuecollection are stronugly typed // With the type that was specified for dictionary values. console. writeline (); foreach (string s in valuecoll) {console. writeline ("value = {0}", S) ;}// to get the keys alone, use the keys property. dictionary <string, string>. keycollection Keycoll = openwith. keys; // The elements of the keycollection are stronugly typed // With the type that was specified for dictionary 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 \ ")"); openwith. remove ("Doc"); If (! Openwith. containskey ("Doc") {console. writeline ("key \" Doc \ "is not found. ") ;}}/ * This code example produces the following output: an element with key =" TXT "already exists. for key = "rtf", value = wordpad.exe. for key = "rtf", value = winword.exe. key = "TIF" is not found. key = "TIF" is not found. value added for key = "ht": hypertrm.exe key = txt, value = notepad.exe key = BMP, value = paint.exe key = Dib, value = paint.exe key = RTF, value = winword.exe key = doc, value = winword.exe key = HT, value = hypertrm.exe value = notepad.exe value = paint.exe value = winword.exe value = hypertrm.exe key = txtkey = BMP key = dibkey = rtfkey = dockey = htremove ("Doc ") key "Doc" is not found. */http://msdn2.microsoft.com/zh-cn/library/xfhwa508 (vs.80 ). aspx