IDictionary<TKey,TValue>数据字典使用讲解

  1. 接口描述

    姑苏网站建设公司成都创新互联公司,姑苏网站设计制作,有大型网站制作公司丰富经验。已为姑苏上1000+提供企业网站建设服务。企业网站搭建\外贸网站制作要多少钱,请找那个售后服务好的姑苏做网站的公司定做!

       Represents a nongeneric collection of key/value pairs.[代表一个非泛型的键/值对的集合]。在System.Collections.Generic包下面。所在程序集为mscorlib.dll中。

  2. 语法

   public Interface IDictionary
:ICollection>,IEnumerable>,IEnumberable
备注   IDictionary 接口是键/值对的泛型集合的基接口。每个元素都是一个存储在 KeyValuePair 对象中的键/值对。每一对都必须有唯一的键。 实现在是否允许 key 为 null 方面有所不同。 此值可以为 null,并且不必是唯一的。 IDictionary 接口允许对所包含的键和值进行枚举,但这并不意味着任何特定的排序顺序。C# 语言中的 foreach 语句(在 Visual Basic 中为 For Each,在 C++ 中为 for each)需要集合中每个元素的类型。 由于 IDictionary 的每个元素都是一个键/值对,因此元素类型既不是键的类型,也不是值的类型。 而是 KeyValuePair 类型。

代码案例如下:

foreach (KeyValuePair kvp in myDictionary)
{
    Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value);
}

    注:foreach 语句是对枚举数的包装,它只允许从集合中读取,不允许写入集合。


    方法列表:

方法名方法描述
Add(T)
将某项添加到 ICollection 中。 (继承自 ICollection。)
Add(TKey,TValue)在 IDictionary 中添加一个带有所提供的键和值的元素。
Clear()
清空ICollection中的所有元素。
Contains确认ICollection集合中是否有特定的值
ContainsKey
确认IDictionary集合中是否包含指定键元素。
CopyTo从特定的 Array 索引开始,将 ICollection 的元素复制到一个 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.
            IDictionary openWith = 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>数据字典使用讲解
当前网址:http://pwwzsj.com/article/ighejg.html