C 中的Dictionary字典類介紹

2022-05-06 17:03:09 字數 3570 閱讀 3267

**鏈結

說明

必須包含名空間system.collection.generic 

dictionary裡面的每乙個元素都是乙個鍵值對(由二個元素組成:鍵和值) 

鍵必須是唯一的,而值不需要唯一的 

鍵和值都可以是任何型別(比如:string, int, 自定義型別,等等) 

通過乙個鍵讀取乙個值的時間是接近o(1) 

鍵值對之間的偏序可以不定義

使用方法

//定義

dictionaryopenwith = new dictionary();

//新增元素

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 = .", openwith["rtf"]);

//更改值

openwith["rtf"] = "winword.exe";

console.writeline("for key = \"rtf\", value = .", openwith["rtf"]);

//遍歷key

foreach (string key in openwith.keys)

", key);

}

//遍歷value

foreach (string value in openwith.values)

", value);

}//遍歷value, second method

dictionary.valuecollection valuecoll = openwith.values;

foreach (string s in valuecoll)

", s);

}

//遍歷字典

foreach (keyvaluepairkvp in openwith)

, value = ", kvp.key, kvp.value);

}

//新增存在的元素

trycatch (argumentexception)

//刪除元素

openwith.remove("doc");

if (!openwith.containskey("doc"))

//判斷鍵存在

if (openwith.containskey("bmp")) // true

引數為其他型別

//引數為其它型別 

dictionaryothertype = new dictionary();

othertype.add(1, "1,11,111".split(','));

othertype.add(2, "2,22,222".split(','));

console.writeline(othertype[1][2]);

引數為自定義型別

首先定義類

class doucube

set } private int _code;

public string page set } private string _page;

}

然後

//宣告並新增元素

dictionarymytype = new dictionary();

for (int i = 1; i <= 9; i++)

//遍歷元素

foreach (keyvaluepairkvp in mytype)

code: page:", 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             獲取當前例項的 type。 (繼承自 object。)

memberwiseclone     建立當前 object 的淺表副本。 (繼承自 object。)

ondeserialization    實現 system.runtime.serialization.iserializable 介面,並在完成反序列化之後引發反序列化事件。

remove              從dictionary中移除所指定的鍵的值。

tostring            返回表示當前物件的字串。 (繼承自 object。)

trygetvalue         獲取與指定的鍵相關聯的值。

C 字典Dictionary在unity中使用案例

c 字典在unity中使用案例 1 前言 講起c dictionary,許多人聞之色變,不了解,不清楚,即使知道,了解,也不一定會用,鑑於此,本人特地總結了乙個使用字典的案例。2 什麼是字典。必須包含名空間system.collection.generic dictionary裡面的每乙個元素都是乙...

C 中的資料字典Dictionary

有50w個int型別的數字,現在需要判斷一下裡面是否存在重複的數字,請簡要說明下。假如這個題目讓我做,第一感覺可能直接向兩個for迴圈,簡單做個判斷就解決了。可是看到幾個大佬的討論,才發現是我知識淺薄了。這道題難道考的就是對業務 迴圈的應用嗎?肯定不是的。我們知道,在驗證一段 或者乙個程式演算法的完...

C 字典 Dictionary 用法

dictionary提供快速的基於鍵值的元素查詢。結構是 dictionary 當你有很多元素的時候可以用它。它包含在system.collections.generic名控制項中。在使用前,你必須宣告它的鍵型別和值型別。1 using system 2 using system.collectio...