我正在处理一段代码,它需要比较字典中的键。如果键匹配,则需要比较值以查看它们是否相同,如果不同,则需要写入键和两个描述(第一个字典的值和第二个字典的值)。我已经了解了TryGetValue函数,但似乎并不是我所需的。有没有一种方法可以检索第二个字典中具有与第一个字典相同键的值?谢谢。
foreach (KeyValuePair<string, string> item in dictionaryOne)
{
if (dictionaryTwo.ContainsKey(item.Key))
{
//Compare values
//if values differ
//Write codes and strings in format
//"Code: " + code + "RCT3 Description: " + rct3Description + "RCT4 Description: " + rct4Description
if (!dictionaryTwo.ContainsValue(item.Value))
{
inBoth.Add("Code: " + item.Key + " RCT3 Description: " + item.Value + " RCT4 Description: " + );
}
}
else
{
//If key doesn't exist
//Write code and string in same format as input file to array
//Array contains items in RCT3 that are not in RCT4
rct3In.Add(item.Key + " " + item.Value);
}
}
TryGetValue
将完全满足你的需求。 - Erti-Chris EelmaaTryGetValue
吗?你能展示一下你如何调用它的例子吗?(这正是你所需要的...)。 - O. R. Mapper