Dictionary的基本用法

2021-10-12 07:33:31 字數 1852 閱讀 6242

1.建立泛型雜湊表,然後加入元素

dictionary<

string

,string

> openwith=

newdictionary

<

string

,string

>()

; openwith.

add(

"txt"

,"notepad.exe");

openwith.

add(

"bmp"

,"paint.exe");

openwith.

add(

"dib"

,"paint.exe");

openwith.

add(

"rtf"

,"wordpad.exe"

);

2.遍歷key

foreach

(string key in openwith.keys)

", key);}

3.遍歷value

foreach

(string

value

in openwith.values)",

value);

}```

4.遍歷value

,second method

```csharp

dictionary<

string

,string

>

.valuecollection valuecoll = openwith.values;

foreach

(string s in valuecoll)

", s)

;}

5.遍歷字典

foreach

(keyvaluepair<

string

,string

> kvp in openwith)

, value = "

, kvp.key, kvp.value)

;}

6.新增存在的sql語句元素

try

catch

(argumentexception

)

7.刪除元素

openwith.

remove

("doc");

if(!openwith.

containskey

("doc"))

8.判斷鍵存在

if(openwith.

containskey

("bmp"))

// true

9.引數為其它c#教程

型別

dictionary<

int,

string

> othertype =

newdictionary

<

int,

string>()

; othertype.

add(1,

"1,11,111"

.split

(','))

; othertype.

add(2,

"2,22,222"

.split

(','))

; console.

writeline

(othertype[1]

[2])

;

Dictionary用法總結

說明必須包含名空間system.collection.generic dictionary裡面的每乙個元素都是乙個鍵值對 由二個元素組成 鍵和值 鍵必須是唯一的,而值不需要唯一的 鍵和值都可以是任何型別 比如 string,int,自定義型別,等等 通過乙個鍵讀取乙個值的時間是接近o 1 鍵值對之間...

C 字典 Dictionary 用法

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

C 字典 Dictionary 用法

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