Dictionary的基本用法

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");

2.遍历key

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


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

 

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

5.遍历字典

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

6.添加存在的sql语句元素

try
            {
                openWith.Add("txt", "winword.exe");
            }
            catch (ArgumentException)
            {
                Console.WriteLine("An element with Key = \"txt\" already exists.");
            }

7.删除元素

openWith.Remove("doc");
            if (!openWith.ContainsKey("doc"))
            {
                Console.WriteLine("Key \"doc\" is not found.");
            }
8.判断键存在

if (openWith.ContainsKey("bmp")) // True 
            {
                Console.WriteLine("An element with Key = \"bmp\" exists.");
            }

9.参数为其它c#教程
类型

Dictionary<int, string[]> OtherType = new Dictionary<int, string[]>();
            OtherType.Add(1, "1,11,111".Split(','));
            OtherType.Add(2, "2,22,222".Split(','));
            Console.WriteLine(OtherType[1][2]);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值