2024年2月18日发(作者:)

C#nary 1 using System; 2 using c; 3

4 public class Example 5 { 6 public static void Main() 7 { 8 // Create a new dictionary of strings, with string keys. 9 // 10 Dictionary openWith =

11 new Dictionary(); 12

13 // Add some elements to the dictionary. There are no

14 // duplicate keys, but some of the values are duplicates. 15 ("txt", ""); 16 ("bmp", ""); 17 ("dib", ""); 18 ("rtf", ""); 19

20 // The Add method throws an exception if the new key is

21 // already in the dictionary. 22 try 23 { 24 ("txt", ""); 25 } 26 catch (ArgumentException) 27 { 28 ine("An element with Key = "txt" already exists."); 29 } 30

31 // The Item property is another name for the indexer, so you

32 // can omit its name when accessing elements.

33 ine("For key = "rtf", value = {0}.",

34 openWith["rtf"]); 35

36 // The indexer can be used to change the value associated 37 // with a key. 38 openWith["rtf"] = ""; 39 ine("For key = "rtf", value = {0}.",

40 openWith["rtf"]); 41

42 // If a key does not exist, setting the indexer for that key 43 // adds a new key/value pair. 44 openWith["doc"] = ""; 45

46 // The indexer throws an exception if the requested key is 47 // not in the dictionary. 48 try 49 { 50 ine("For key = "tif", value = {0}.",

51 openWith["tif"]); 52 } 53 catch (KeyNotFoundException) 54 { 55 ine("Key = "tif" is not found."); 56 } 57

58 // When a program often has to try keys that turn out not to 59 // be in the dictionary, TryGetValue can be a more efficient

60 // way to retrieve values. 61 string value = ""; 62 if (Value("tif", out value)) 63 { 64 ine("For key = "tif", value = {0}.", value); 65 } 66 else 67 { 68 ine("Key = "tif" is not found."); 69 } 70

71 // ContainsKey can be used to test keys before inserting

72 // them. 73 if (!nsKey("ht")) 74 { 75 ("ht", ""); 76 ine("Value added for key = "ht": {0}",

77 openWith["ht"]); 78 } 79

80 // When you use foreach to enumerate dictionary elements,

81 // the elements are retrieved as KeyValuePair objects. 82 ine(); 83 foreach( KeyValuePair kvp in openWith ) 84 { 85 ine("Key = {0}, Value = {1}",

86 , ); 87 } 88

89 // To get the values alone, use the Values property. 90 Dictionary.ValueCollection valueColl = 91 ; 92

93 // The elements of the ValueCollection are strongly typed 94 // with the type that was specified for dictionary values. 95 ine(); 96 foreach( string s in valueColl ) 97 { 98 ine("Value = {0}", s); 99 }100

101 // To get the keys alone, use the Keys property.102 Dictionary.KeyCollection keyColl =103 ;104

105 // The elements of the KeyCollection are strongly typed106 // with the type that was specified for dictionary keys.107 ine();108 foreach( string s in keyColl )109 {110 ine("Key = {0}", s);111 }112

113 // Use the Remove method to remove a key/value pair.114 ine("nRemove("doc")");115 ("doc");116

117 if (!nsKey("doc"))118 {119 ine("Key "doc" is not found.");120 }121 }122 }123

124 /* This code example produces the following output:125

126 An element with Key = "txt" already exists.127 For key = "rtf", value = .128 For key = "rtf", value = .129 Key = "tif" is not found.130 Key = "tif" is not found.131 Value added for key = "ht": 132

133 Key = txt, Value = 134 Key = bmp, Value = 135 Key = dib, Value = 136 Key = rtf, Value = 137 Key = doc, Value = 138 Key = ht, Value = 139

140 Value = 141 Value = 142 Value = 143 Value = 144 Value = 145 Value = 146

147 Key = txt148 Key = bmp149 Key = dib150 Key = rtf151 Key = doc152 Key = ht153

154 Remove("doc")155 Key "doc" is not found.156 */

1 using System; 2 using c; 3 public class Example 4 { 5 public static void Main()

(Inherited from )获取与指定键关联的值。