IDictionary<TKey,TValue>數(shù)據(jù)字典使用講解

  1. 接口描述

    姑蘇網(wǎng)站建設(shè)公司成都創(chuàng)新互聯(lián)公司,姑蘇網(wǎng)站設(shè)計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗。已為姑蘇上1000+提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)網(wǎng)站制作要多少錢,請找那個售后服務(wù)好的姑蘇做網(wǎng)站的公司定做!

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

  2. 語法

   public Interface IDictionary<TKey,TValue>
:ICollection<KeyValuePair<TKey,TValue>>,IEnumerable<KeyValuePair<TKey,TValue>>,IEnumberable
備注   IDictionary<TKey, TValue> 接口是鍵/值對的泛型集合的基接口。每個元素都是一個存儲在 KeyValuePair<TKey, TValue> 對象中的鍵/值對。每一對都必須有唯一的鍵。 實現(xiàn)在是否允許 key 為 null 方面有所不同。 此值可以為 null,并且不必是唯一的。 IDictionary<TKey, TValue> 接口允許對所包含的鍵和值進行枚舉,但這并不意味著任何特定的排序順序。C# 語言中的 foreach 語句(在 Visual Basic 中為 For Each,在 C++ 中為 for each)需要集合中每個元素的類型。 由于 IDictionary<TKey, TValue> 的每個元素都是一個鍵/值對,因此元素類型既不是鍵的類型,也不是值的類型。 而是 KeyValuePair<TKey, TValue> 類型。

代碼案例如下:

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

    注:foreach 語句是對枚舉數(shù)的包裝,它只允許從集合中讀取,不允許寫入集合。


    方法列表:

方法名方法描述
Add(T)
將某項添加到 ICollection<T> 中。 (繼承自 ICollection<T>。)
Add(TKey,TValue)在 IDictionary<TKey, TValue> 中添加一個帶有所提供的鍵和值的元素。
Clear()
清空ICollection<T>中的所有元素。
Contains確認ICollection<T>集合中是否有特定的值
ContainsKey
確認IDictionary<TKey, TValue>集合中是否包含指定鍵元素。
CopyTo從特定的 Array 索引開始,將 ICollection<T> 的元素復制到一個 Array 中。 (繼承自 ICollection<T>。)
GeEnumurator
返回一個循環(huán)訪問集合的枚舉器。 (繼承自 IEnumerable<T>。)
Remove(T)
移除指定元素
Remove(TKey)移除指定鍵的元素
TryGetValue獲得與指定鍵關(guān)聯(lián)的元素值

注:擴展方法可到官方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<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");
            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<string, string> kvp in openWith )
            {
                Console.WriteLine("Key = {0}, Value = {1}", 
                    kvp.Key, kvp.Value);
            }

            // To get the values alone, use the Values property.
            ICollection<string> 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>數(shù)據(jù)字典使用講解
本文網(wǎng)址:http://muchs.cn/article26/ighejg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)、網(wǎng)站導航、網(wǎng)站維護、網(wǎng)站收錄、域名注冊、建站公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站托管運營