首頁 後端開發 C#.Net教程 Dictionary字典類別在C#中的範例程式碼介紹

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

May 07, 2017 am 10:17 AM

說明
    必須包含名空間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中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
2 週前 By 尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

使用 C# 的活動目錄 使用 C# 的活動目錄 Sep 03, 2024 pm 03:33 PM

使用 C# 的活動目錄

C# 中的存取修飾符 C# 中的存取修飾符 Sep 03, 2024 pm 03:24 PM

C# 中的存取修飾符

C# 中的隨機數產生器 C# 中的隨機數產生器 Sep 03, 2024 pm 03:34 PM

C# 中的隨機數產生器

C# 資料網格視圖 C# 資料網格視圖 Sep 03, 2024 pm 03:32 PM

C# 資料網格視圖

C# 字串讀取器 C# 字串讀取器 Sep 03, 2024 pm 03:23 PM

C# 字串讀取器

C# 中的模式 C# 中的模式 Sep 03, 2024 pm 03:33 PM

C# 中的模式

C# 字串編寫器 C# 字串編寫器 Sep 03, 2024 pm 03:23 PM

C# 字串編寫器

C# 中的二進位編寫器 C# 中的二進位編寫器 Sep 03, 2024 pm 03:22 PM

C# 中的二進位編寫器

See all articles