IDictionary<TKey,TValue>数据字典使用讲解
接口描述
姑苏网站建设公司成都创新互联公司,姑苏网站设计制作,有大型网站制作公司丰富经验。已为姑苏上1000+提供企业网站建设服务。企业网站搭建\外贸网站制作要多少钱,请找那个售后服务好的姑苏做网站的公司定做!
Represents a nongeneric collection of key/value pairs.[代表一个非泛型的键/值对的集合]。在System.Collections.Generic包下面。所在程序集为mscorlib.dll中。
语法
public Interface IDictionary
:ICollection>,IEnumerable>,IEnumberable
备注 IDictionary代码案例如下:
foreach (KeyValuePairkvp in myDictionary) { Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value); }
注:foreach 语句是对枚举数的包装,它只允许从集合中读取,不允许写入集合。
方法列表:
方法名 | 方法描述 |
Add(T) | 将某项添加到 ICollection |
Add(TKey,TValue) | 在 IDictionary |
Clear() | 清空ICollection |
Contains | 确认ICollection |
ContainsKey | 确认IDictionary |
CopyTo | 从特定的 Array 索引开始,将 ICollection |
GeEnumurator | 返回一个循环访问集合的枚举器。 (继承自 IEnumerable |
Remove(T) | 移除指定元素 |
Remove(TKey) | 移除指定键的元素 |
TryGetValue | 获得与指定键关联的元素值 |
注:扩展方法可到官方MSDN查看:http://msdn.microsoft.com/zh-cn/library/8hyehyw5(v=vs.110).aspx
// Create a new dictionary of strings, with string keys, // and access it through the IDictionary generic interface. IDictionaryopenWith = new Dictionary (); // 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"); openWith.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 requested 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)) { Console.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 kvp in openWith ) { Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value); } // To get the values alone, use the Values property. ICollection icoll = openWith.Values; // The elements of the ValueCollection are strongly typed // with the type that was specified for dictionary values. Console.WriteLine(); foreach( string s in icoll ) { Console.WriteLine("Value = {0}", s); } // To get the keys alone, use the Keys property. icoll = openWith.Keys; // The elements of the ValueCollection are strongly typed // with the type that was specified for dictionary values. Console.WriteLine(); foreach( string s in icoll ) { 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."); } Console.ReadLine();
新闻名称:IDictionary<TKey,TValue>数据字典使用讲解
分享URL:http://hbruida.cn/article/ighejg.html