DictionaryGeneric and Hashtable

本文通过示例介绍了如何使用C#中的Dictionary类进行基本操作,包括添加、插入、迭代等常见用法。
Dictionary and Hashtable Class Diagrams

Adding Items to a Dictionary<TKey, TValue> 
ContractedBlock.gifExpandedBlockStart.gifcode
 1None.gifusing System; 
 2None.gif   using System.Collections.Generic; 
 3None.gif
 4None.gif   class Program 
 5ExpandedBlockStart.gifContractedBlock.gif   dot.gif
 6InBlock.gif     static void Main() 
 7ExpandedSubBlockStart.gifContractedSubBlock.gif     dot.gif
 8InBlock.gif
 9InBlock.gif         Dictionary<Guid,string> dictionary = 
10InBlock.gif              newDictionary<Guid, string>(); 
11InBlock.gif         Guid key = Guid.NewGuid(); 
12InBlock.gif
13InBlock.gif         dictionary.Add(key, "object"); 
14ExpandedSubBlockEnd.gif     }
 
15ExpandedBlockEnd.gif   }
 
Inserting Items in a Dictionary<TKey, TValue> Using the Index Operator
ContractedBlock.gifExpandedBlockStart.gif代码
 1None.gifusing System;
 2None.gifusing System.Collections.Generic;
 3None.gif
 4None.gifclass Program
 5ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 6InBlock.gif    static void Main()
 7ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 8InBlock.gif        Dictionary<Guid, string> dictionary = new Dictionary<Guid, string>();
 9InBlock.gif        Guid key = Guid.NewGuid();
10InBlock.gif
11InBlock.gif        dictionary[key] = "object";
12InBlock.gif        dictionary[key] = "byte";
13ExpandedSubBlockEnd.gif    }

14ExpandedBlockEnd.gif}
Iterating over Dictionary<TKey, TValue> with foreach
ContractedBlock.gifExpandedBlockStart.gifcode
 1None.gifusing System;
 2None.gifusing System.Collections.Generic;
 3None.gifclass Program
 4ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 5InBlock.gif
 6InBlock.gif    static void Main()
 7ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 8InBlock.gif        Dictionary<stringstring> dictionary = new
 9InBlock.gif             Dictionary<stringstring>();
10InBlock.gif        int index = 0;
11InBlock.gif        dictionary.Add(index++.ToString(), "object");
12InBlock.gif        dictionary.Add(index++.ToString(), "byte");
13InBlock.gif        dictionary.Add(index++.ToString(), "uint");
14InBlock.gif        dictionary.Add(index++.ToString(), "ulong");
15InBlock.gif        dictionary.Add(index++.ToString(), "float");
16InBlock.gif        dictionary.Add(index++.ToString(), "char");
17InBlock.gif        dictionary.Add(index++.ToString(), "bool");
18InBlock.gif        dictionary.Add(index++.ToString(), "ushort");
19InBlock.gif        dictionary.Add(index++.ToString(), "decimal");
20InBlock.gif        dictionary.Add(index++.ToString(), "int");
21InBlock.gif        dictionary.Add(index++.ToString(), "sbyte");
22InBlock.gif        dictionary.Add(index++.ToString(), "short");
23InBlock.gif        dictionary.Add(index++.ToString(), "long");
24InBlock.gif        dictionary.Add(index++.ToString(), "void");
25InBlock.gif        dictionary.Add(index++.ToString(), "double");
26InBlock.gif        dictionary.Add(index++.ToString(), "string");
27InBlock.gif
28InBlock.gif        Console.WriteLine("Key  Value   Hashcode");
29InBlock.gif        Console.WriteLine("---  ------  --------");
30InBlock.gif        foreach (KeyValuePair<stringstring> i in dictionary)
31ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
32InBlock.gif            Console.WriteLine("{0,-5}{1,-9}{2}",
33InBlock.gif                i.Key, i.Value, i.Key.GetHashCode());
34ExpandedSubBlockEnd.gif        }

35InBlock.gif
36InBlock.gif        Console.ReadKey();
37ExpandedSubBlockEnd.gif    }

38ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/nanshouyong326/archive/2007/05/11/742492.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值