提前致歉;我在嘗試學(xué)習(xí) C# 的第 3 天。我被指示建立一個(gè)字典哈希集;沒(méi)問(wèn)題。我看到那已經(jīng)建成了。如果字典的鍵 != 特定字符串,我現(xiàn)在需要遍歷哈希集并將條目復(fù)制到新列表。有人可以解釋一下實(shí)現(xiàn)這個(gè)看似簡(jiǎn)單的任務(wù)的正確語(yǔ)法嗎?var goodSongs = new List<Dictionary<string,string>>();var allSongs = new HashSet<Dictionary<string, string>>();Dictionary<string, string> meatloaf = new Dictionary<string, string>();meatloaf.Add("Meatloaf", "Paradise By The Dashboard Light");Dictionary<string, string> aerosmith = new Dictionary<string,string>();aerosmith.Add("Aerosmith", "Cryin'");Dictionary<string, string> nickelback = new Dictionary<string, string>();nickelback.Add("Nickelback", "Rockstar");allSongs.Add(nickelback);allSongs.Add(aerosmith);allSongs.Add(meatloaf);//foreach loop to iterate dictionaries goes here目標(biāo) - 擺脫困境,希望學(xué)習(xí) C#,并決定我是否要繼續(xù)探索這個(gè)兔子洞。謝謝大家。
1 回答

天涯盡頭無(wú)女友
TA貢獻(xiàn)1831條經(jīng)驗(yàn) 獲得超9個(gè)贊
這是一個(gè)如何遍歷哈希集然后遍歷字典的示例:
var all = new HashSet<Dictionary<string, string>>();
Dictionary<string, string> newDict = new Dictionary<string, string>();
newDict.Add("M", "T");
all.Add(newDict);
foreach(Dictionary<string,string> dict in all)
{
foreach(KeyValuePair<string,string> pair in dict)
{
string key = pair.Key;
string value = pair.Value;
}
}
- 1 回答
- 0 關(guān)注
- 133 瀏覽
添加回答
舉報(bào)
0/150
提交
取消