protected void Page_Load(object sender, EventArgs e)
{
IList<string> AList = new List<string>();
AList.Add("KimhillZhangA");
AList.Add("KimhillZhangB");
AList.Add("KimhillZhangC");
IList<string> BList = new List<string>();
BList.Add("KimhillZhangD");
BList.Add("KimhillZhangE");
BList.Add("KimhillZhangF");
Dictionary<string, IList<string>> dictionary = new Dictionary<string, IList<string>>();
//添加键/值,方法一
dictionary.Add("A", AList);
//添加键/值,方法二
dictionary["B"] = BList;
//遍历键
foreach (string key in dictionary.Keys)
{
//遍历某键的值
foreach (string val in dictionary[key])
{
}
}
//取某键的值,,使用 TryGetValue 方法作为一种更有效的方法来检索值
if (dictionary.TryGetValue("A", out AList))
{
for (int i = 0; i < AList.Count; i++)
{
Response.Write(AList[i]);
}
}
//是否包含某键
if (!dictionary.ContainsKey("A"))
{
dictionary.Add("A", AList);
}
//由于基于 IDictionary 的集合中的每个元素都是一个键/值对,因此元素类型既不是键的类型,也不是值的类型。而是 KeyValuePair 类型
foreach (KeyValuePair<string, IList<string>> kvp in dictionary)
{
string key = kvp.Key;//key包含了两个键,A,B
for (int i = 0; i < kvp.Value.Count; i++)
{
Response.Write(kvp.Value[i]);
}
}
//定义一个<string,int>的Dictionary,让它的值进行添加
Dictionary<string,int> dic = new Dictionary<string,int>();
//添加两个键为"成绩1","成绩2";并为它们的值赋为0
dic["成绩1"] = 0;
dic["成绩2"] = 0;
// 把这两个值分别加1
dic["成绩1"]++;
dic["成绩2"]++;
}