提前致歉;我正在尝试学习C#的第3天。
我被指示建立字典的哈希表;没问题。我看到那已经建立。我现在需要遍历哈希集,如果字典的键!=特定字符串,则将条目复制到新列表中。有人可以解释一下完成此看似简单任务的正确语法吗?
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
Run Code Online (Sandbox Code Playgroud)
目标-要摆脱困境,希望学习C#,并决定我是否要继续努力下去。谢谢大家。
这是一个如何遍历哈希集然后遍历字典的示例:
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;
}
}
Run Code Online (Sandbox Code Playgroud)