????c#???????????? for??foreach
????for?? ????????λ??????β????????????? for????????п????????????? ???????????????壬???????
????foreach?? ??????ienumerator???? ??????в??????????????? ?????????????'++' ?? ?????Ч????
????Dictionary?????????
Dictionary<string?? int> list = new Dictionary<string?? int>();
list.Add("d"?? 1);
//3.0????汾
foreach (var item in list)
{
Console.WriteLine(item.Key + item.Value);
}
//KeyValuePair<T??K>
foreach (KeyValuePair<string?? int> kv in list)
{
Console.WriteLine(kv.Key + kv.Value);
}
//???????????
foreach (string key in list.Keys)
{
Console.WriteLine(key + list[key]);
}
//?????
foreach (int val in list.Values)
{
Console.WriteLine(val);
}
//???????for????????
List<string> test = new List<string>(list.Keys);
for (int i = 0; i < list.Count; i++)
{
Console.WriteLine(test[i] + list[test[i]]);
}
????List????
Hashtable ht=new Hashtable();
ht.Add("E"??"e");
ht.Add("A"??"a");
ht.Add("C"??"c");
ht.Add("B"??"b");
ArrayList lst=new ArrayList(ht.Keys);
lst.Sort();
foreach(string key in lst)
{
listBox1.Items.Add("key:" + key + "  vlaue:"+ht[key]);
}
????Dictionary????
?????????·??
????1>?????List????Dictionary??????
????2>???μ?List????????
????3>??List????????????????????Dictionary
protected Dictionary<string?? int> SortDictionary_Desc(Dictionary<string?? int> dic)
{
List<KeyValuePair<string?? int>> myList = new List<KeyValuePair<string?? int>>(dic);
myList.Sort(delegate(KeyValuePair<string?? int> s1?? KeyValuePair<string?? int> s2)
{
return s2.Value.CompareTo(s1.Value);
});
dic.Clear();
foreach (KeyValuePair<string?? int> pair in myList)
{
dic.Add(pair.Key?? pair.Value);
}
return dic;
}
protected Dictionary<string?? int> SortDictionary_Asc(Dictionary<string?? int> dic)
{
List<KeyValuePair<string?? int>> myList = new List<KeyValuePair<string?? int>>(dic);
myList.Sort(delegate(KeyValuePair<string?? int> s1?? KeyValuePair<string?? int> s2)
{
return s1.Value.CompareTo(s2.Value);
});
dic.Clear();
foreach (KeyValuePair<string?? int> pair in myList)
{
dic.Add(pair.Key?? pair.Value);
}
return dic;
}