C#Dictionary的描述
Dictionary的描述 1、从一组键(Key)到一组值(Value)的映射,每一个添加项都是由一个值及其相关连的键组成 2、任何键都必须是唯一的 3、键不能为空引用null(VB中的Nothing),若值为引用类型,则可以为空值 4、Key和Value可以是任何类型(string,int,custom class 等) 类似配置文件 //使用字符串键创建一个新的字符串字典。 Dictionary<string, string> openWith = new Dictionary<string, string>(); //将一些元素添加到字典中。 没有重复的键,但有些值是重复的。 openWith.Add("txt", "notepad.exe"); openWith.Add("bmp", "paint.exe"); openWith.Add("dib", "paint.exe"); openWith.Add("rtf", "wordpad.exe"); // 如果新键已经在字典中,Add 方法会抛出异常。 try { openWith.Add("txt", "winword.exe"); } catch (ArgumentException) { Console.WriteLine("An element with Key = \"txt\" already exists."); } // Item 属性是索引器的另一个名称,因此在访问元素时可以省略它的名称。 Console.WriteLine("For key = \"rtf\", value = {0}.", openWith["rtf"]); // 索引器可用于更改与键关联的值.. openWith["rtf"] = "winword.exe"; Console.WriteLine("For key = \"rtf\", value = {0}.", openWith["rtf"]); // 如果键不存在,则为该键设置索引器会添加一个新的键/值对。 openWith["doc"] = "winword.exe"; // 如果请求的键不在字典中,索引器会抛出异常。 try { Console.WriteLine("For key = \"tif\", value = {0}.", openWith["tif"]); } catch (KeyNotFoundException) { Console.WriteLine("Key = \"tif\" is not found."); } // 当程序经常不得不尝试结果不在字典中的键时,TryGetValue 可能是一种更有效的检索值的方法。 string value = ""; if (openWith.TryGetValue("tif", out value)) { Console.WriteLine("For key = \"tif\", value = {0}.", value); } else { Console.WriteLine("Key = \"tif\" is not found."); } // ContainsKey 可用于在插入之前测试密钥。 if (!openWith.ContainsKey("ht")) { openWith.Add("ht", "hypertrm.exe"); Console.WriteLine("Value added for key = \"ht\": {0}", openWith["ht"]); } // 当你使用 foreach 枚举字典元素时,元素被检索为 KeyValuePair 对象。 Console.WriteLine(); foreach( KeyValuePair<string, string> kvp in openWith ) { Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value); } // 要单独获取值,请使用 Values 属性。 Dictionary<string, string>.ValueCollection valueColl = openWith.Values; // ValueCollection 的元素使用为字典值指定的类型进行强类型化。 Console.WriteLine(); foreach( string s in valueColl ) { Console.WriteLine("Value = {0}", s); } // 要单独获取密钥,请使用 Keys 属性。 Dictionary<string, string>.KeyCollection keyColl = openWith.Keys; // // KeyCollection 的元素使用为字典键指定的类型进行强类型化。 Console.WriteLine(); foreach( string s in keyColl ) { Console.WriteLine("Key = {0}", s); } // 使用 Remove 方法删除键/值对。 Console.WriteLine("\nRemove(\"doc\")"); openWith.Remove("doc"); if (!openWith.ContainsKey("doc")) { Console.WriteLine("Key \"doc\" is not found."); } /* 此代码示例产生以下输出: 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 = paint.exe Value = winword.exe Value = winword.exe Value = hypertrm.exe Key = txt Key = bmp Key = dib Key = rtf Key = doc Key = ht Remove("doc") Key "doc" is not found. */
Dictionary的描述 1、从一组键(Key)到一组值(Value)的映射,每一个添加项都是由一个值及其相关连的键组成 2、任何键都必须是唯一的 3、键不能为空引用null(VB中的Nothing),若值为引用类型,则可以为空值 4、Key和Value可以是任何类型(string,int,custom class 等) 类似配置文件 //使用字符串键创建一个新的字符串字典。 Dictionary<string, string> openWith = new Dictionary<string, string>(); //将一些元素添加到字典中。 没有重复的键,但有些值是重复的。 openWith.Add("txt", "notepad.exe"); openWith.Add("bmp", "paint.exe"); openWith.Add("dib", "paint.exe"); openWith.Add("rtf", "wordpad.exe"); // 如果新键已经在字典中,Add 方法会抛出异常。 try { openWith.Add("txt", "winword.exe"); } catch (ArgumentException) { Console.WriteLine("An element with Key = \"txt\" already exists."); } // Item 属性是索引器的另一个名称,因此在访问元素时可以省略它的名称。 Console.WriteLine("For key = \"rtf\", value = {0}.", openWith["rtf"]); // 索引器可用于更改与键关联的值.. openWith["rtf"] = "winword.exe"; Console.WriteLine("For key = \"rtf\", value = {0}.", openWith["rtf"]); // 如果键不存在,则为该键设置索引器会添加一个新的键/值对。 openWith["doc"] = "winword.exe"; // 如果请求的键不在字典中,索引器会抛出异常。 try { Console.WriteLine("For key = \"tif\", value = {0}.", openWith["tif"]); } catch (KeyNotFoundException) { Console.WriteLine("Key = \"tif\" is not found."); } // 当程序经常不得不尝试结果不在字典中的键时,TryGetValue 可能是一种更有效的检索值的方法。 string value = ""; if (openWith.TryGetValue("tif", out value)) { Console.WriteLine("For key = \"tif\", value = {0}.", value); } else { Console.WriteLine("Key = \"tif\" is not found."); } // ContainsKey 可用于在插入之前测试密钥。 if (!openWith.ContainsKey("ht")) { openWith.Add("ht", "hypertrm.exe"); Console.WriteLine("Value added for key = \"ht\": {0}", openWith["ht"]); } // 当你使用 foreach 枚举字典元素时,元素被检索为 KeyValuePair 对象。 Console.WriteLine(); foreach( KeyValuePair<string, string> kvp in openWith ) { Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value); } // 要单独获取值,请使用 Values 属性。 Dictionary<string, string>.ValueCollection valueColl = openWith.Values; // ValueCollection 的元素使用为字典值指定的类型进行强类型化。 Console.WriteLine(); foreach( string s in valueColl ) { Console.WriteLine("Value = {0}", s); } // 要单独获取密钥,请使用 Keys 属性。 Dictionary<string, string>.KeyCollection keyColl = openWith.Keys; // // KeyCollection 的元素使用为字典键指定的类型进行强类型化。 Console.WriteLine(); foreach( string s in keyColl ) { Console.WriteLine("Key = {0}", s); } // 使用 Remove 方法删除键/值对。 Console.WriteLine("\nRemove(\"doc\")"); openWith.Remove("doc"); if (!openWith.ContainsKey("doc")) { Console.WriteLine("Key \"doc\" is not found."); } /* 此代码示例产生以下输出: 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 = paint.exe Value = winword.exe Value = winword.exe Value = hypertrm.exe Key = txt Key = bmp Key = dib Key = rtf Key = doc Key = ht Remove("doc") Key "doc" is not found. */
浙公网安备 33010602011771号