Dictionary字典類別在C#中的範例程式碼介紹

黄舟
發布: 2017-05-07 10:17:14
原創
1647 人瀏覽過

說明
    必須包含名空間System.Collection.Generic 
    Dictionary裡面的每一個元素都是一個鍵值對(由二個元素組成:鍵與值) 
#    鍵必須是唯一的,而值不需要唯一的 
    鍵和值都可以是任何類型(例如:string, int, 自訂類型,等等) 
    透過一個鍵讀取一個值的時間是接近O(1) 
    鍵值對之間的偏序可以不定義

使用方法

    //定义
    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");
 

    //取值
    Console.WriteLine("For key = \"rtf\", value = {0}.", openWith["rtf"]);
 

    //更改值
    openWith["rtf"] = "winword.exe";
    Console.WriteLine("For key = \"rtf\", value = {0}.", openWith["rtf"]);
 

    //遍历key
    foreach (string key in openWith.Keys)
    {
        Console.WriteLine("Key = {0}", key);
    }
 

    //遍历value
    foreach (string value in openWith.Values)
    {
        Console.WriteLine("value = {0}", value);
    }

    //遍历value, Second Method
    Dictionary<string, string>.ValueCollection valueColl = openWith.Values;
    foreach (string s in valueColl)
    {
        Console.WriteLine("Second Method, Value = {0}", s);
    }

 

    //遍历字典
    foreach (KeyValuePair<string, string> kvp in openWith)
    {
        Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value);
    }
 

    //添加存在的元素
    try
    {
        openWith.Add("txt", "winword.exe");
    }
    catch (ArgumentException)
    {
        Console.WriteLine("An element with Key = \"txt\" already exists.");
    }
 

    //删除元素
    openWith.Remove("doc");
    if (!openWith.ContainsKey("doc"))
    {
        Console.WriteLine("Key \"doc\" is not found.");
    }
 
    //判断键存在
    if (openWith.ContainsKey("bmp")) // True 
    {
        Console.WriteLine("An element with Key = \"bmp\" exists.");
    }

    //参数为其它类型 
    Dictionary<int, string[]> OtherType = new Dictionary<int, string[]>();
    OtherType.Add(1, "1,11,111".Split(&#39;,&#39;));
    OtherType.Add(2, "2,22,222".Split(&#39;,&#39;));
    Console.WriteLine(OtherType[1][2]);
 

//参数为自定义类型

//首先定义类

    class DouCube
    {
        public int Code { get { return _Code; } set { _Code = value; } } private int _Code;
        public string Page { get { return _Page; } set { _Page = value; } } private string _Page;
    } 

    //然后声明并添加元素
    Dictionary<int, DouCube> MyType = new Dictionary<int, DouCube>();
    for (int i = 1; i <= 9; i++)
    {
        DouCube element = new DouCube();
        element.Code = i * 100;
        element.Page = "http://www.doucube.com/" + i.ToString() + ".html";
        MyType.Add(i, element);
    }

    //遍历元素
    foreach (KeyValuePair<int, DouCube> kvp in MyType)
    {
        Console.WriteLine("Index {0} Code:{1} Page:{2}", kvp.Key, kvp.Value.Code, kvp.Value.Page);
    }
登入後複製

常用屬性

    名稱    說明
    Comparer     取得用於判斷字典中的鍵是否相等的IEqualityComparer
    Count        取得包含在 Dictionary 中的鍵/值對的數目。
    Item         以取得或設定與指定的鍵為相關的值。
    Keys         取得包含 Dictionary 中的鍵的集合。
    Values       以取得包含 Dictionary 中的數值的集合。

常用方法
    名稱    說明
    Add                 則將指定的鍵和值新增至字典。
    Clear              則從 Dictionary 移除所有的鍵與數值。
    ContainsKey         決定 Dictionary 是否包含指定的鍵。
    ContainsValue       決定 Dictionary 是否包含特定數值。
    Equals(Object)      決定指定的 Object 是否等於目前的 Object。 (繼承自 Object。)
    Finalize            允許物件在「垃圾回收」回收前嘗試釋放資源並執行其他清除作業。 (繼承自 Object。)
    GetEnumerator       返回循環存取 Dictionary 的枚舉器。
    GetHashCode         用作特定類型的雜湊函數。 (繼承自 Object。)
    GetObjectData       實作 System.Runtime.Serialization.ISerializable 接口,並傳回序列化 Dictionary 實例所需的資料。
    GetType             以取得目前執行個體的 類型。 (繼承自 Object。)
    MemberwiseClone     建立目前 Object 的淺表副本。 (繼承自 Object。)
    OnDeserialization    實作 System.Runtime.Serialization.ISerializable 介面,並在完成反序列化後引發反序列化事件。
    Remove             則從 Dictionary 移除所指定的鍵的值。
    ToString            以「目前物件」傳回表示目前物件的字串。 (繼承自 Object。)
    TryGetValue         以取得與指定的鍵為關聯的值。

以上是Dictionary字典類別在C#中的範例程式碼介紹的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!