1.建立泛型雜湊表,然後加入元素
2.遍歷keydictionary<
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"
);
5.遍歷字典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)
;}
6.新增存在的sql語句元素foreach
(keyvaluepair<
string
,string
> kvp in openwith)
, value = "
, kvp.key, kvp.value)
;}
7.刪除元素try
catch
(argumentexception
)
9.引數為其它c#教程openwith.
remove
("doc");
if(!openwith.
containskey
("doc"))
8.判斷鍵存在
if(openwith.
containskey
("bmp"))
// true
型別
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...