C#如何對Dictionary遍歷賦值
C#對Dictionary遍歷賦值
Dictionary<int, string> datatable = new Dictionary<int, string>(); datatable.Add(1, "hello1"); datatable.Add(2, "hello2"); int[] keys = datatable.Keys.ToArray(); for (int i = 0; i < keys.Length; i++) { if (datatable[keys[i]] == "hello2") { datatable[keys[i]] = "hello"; } }
C#循環(huán)遍歷Dictionary,并且可以改變值
通常的C#循環(huán)遍歷是用foreach循環(huán)遍歷。好處就是簡單,但是缺點就是無法在循環(huán)中改變Dictionary的值。
Dictionary <string,int>
一下遍歷這個Dictionary。
代碼:
for (int i = 0; i < HostMessage.Count; i++) { var item = HostMessage.ElementAt(i); Console.WriteLine("key is " + item.Key); Console.WriteLine("value is " + item.Value); if (item.Key == "MS01") { HostMessage["MT01"] = 102; } }
默認HostMessage中有值。
這樣就可以了。
我這樣寫有個BUG,就是在終端輸出依舊是沒有改變值的,其實是改變了,是不過沒有及時輸出。要是用以下代碼就好了。
for (int i = 0; i < HostMessage.Count; i++) { var item = HostMessage.ElementAt(i); Console.WriteLine("key is " + item.Key); Console.WriteLine("value is " + item.Value); if (item.Key == "MS01") { HostMessage["MT01"] = 102; } } Console.WriteLine("*****************************8888"); foreach (string key in HostMessage.Keys) { Console.WriteLine("Key = {0}, Value = {1}", key, HostMessage[key]); }
補充:
更新一個新用法
Dictionary<Dictionary<string, int>, string> zh = new Dictionary<Dictionary<string, int>, string>();
如何輸出這個字典?
直接上示例代碼
Dictionary<Dictionary<string, int>, string> zh = new Dictionary<Dictionary<string, int>, string>(); Dictionary<string, int> HostM = new Dictionary<string, int>(); Dictionary<string, string> ss = new Dictionary<string, string>(); HostM.Add("MT01", 101); HostM.Add("MT09",100); HostM.Add("MT010", 101); HostM.Add("MT02", 100); HostM.Add("MT03", 101); HostM.Add("MT04", 100); HostM.Add("MT05", 101); HostM.Add("MT08", 100); HostM.Add("Ms01", 101); HostM.Add("Ms02", 100); HostM.Add("LD01", 101); HostM.Add("PS01", 100); ss.Add("MT01", "traytab"); ss.Add("MT09", "solidifyOut"); ss.Add("MT010", "solidifyIn"); ss.Add("MT02", "WBIn"); ss.Add("MT03", "WBOUt"); ss.Add("MT04", "Finish"); ss.Add("MT05", "are"); ss.Add("MT08", "spc"); ss.Add("Ms01", "LDmachine"); ss.Add("Ms02", "qwe"); ss.Add("LD01", "asc"); ss.Add("PS01", "sdf"); foreach (string key in HostM.Keys) { Dictionary<string, int> ac = new Dictionary<string, int>(); ac.Add(key, HostM[key]); zh.Add(ac, ss[key]); } foreach (Dictionary<string, int> key in zh.Keys) { foreach (string kkey in key.Keys) { Console.WriteLine(kkey + "_" + key[kkey] + "_" + zh[key]); }
這樣就可以了。
前面的都是給字典添加內容的,就不說了,解釋最后三行代碼
foreach (Dictionary<string, int> key in zh.Keys) { foreach (string kkey in key.Keys) { Console.WriteLine(kkey + "_" + key[kkey] + "_" + zh[key]); }
第一行,因為zh的key是一個字典,所以foreach的key要設置為Dictionary<string, int> ,第二foreach的kkey因為Dictionary<string, int> 的key是string,所以設置kkey為string。
這樣思路就清晰了,kkey是(Dictionary<string, int> 的key(鍵)key是Dictionary<string, int>字典,所以值為key[kkey].zh[key]為最外圍的字典內容。這樣只要思路理清了,就輸出正確了。
我在這里繞了好久才想明白。
總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
C#使用SQL DataAdapter數(shù)據(jù)適配代碼實例
今天小編就為大家分享一篇關于C#使用SQL DataAdapter數(shù)據(jù)適配代碼實例,小編覺得內容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-10-10C#中加載dll并調用其函數(shù)的實現(xiàn)方法
下面小編就為大家?guī)硪黄狢#中加載dll并調用其函數(shù)的實現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02