【发布时间】:2017-05-26 05:41:12
【问题描述】:
一个标准的字典应该是这样的:
public Dictionary<int, DictionarySetup> H = new Dictionary<int, DictionarySetup>()
{
{18000, new DictionarySetup { Some values }},
};
从 A 到 T,所有这些都在一个名为 DictionaryInit 的类中,现在我检查该值是否与此布尔值匹配:
public Boolean Dictionary_Test(Dictionary<int, DictionarySetup> AccountLexicon)
{
DictionarySetup ClassValues;
if (AccountLexicon.TryGetValue(MapKey, out ClassValues))
{
return true;
}
return false;
}
现在,我正在寻找一种更有效的方法来遍历每个字典,如果有匹配项,则获取该特定字典以在后续方法中使用,这就是它现在在 if/else 中的样子:
if(Dictionary_Test(theDictionary.C) == true)
{
Dictionary_Find(Account_String, rowindex, theBSDictionary.C, Cash_Value, txtCurrency.Text);
}
else if (Dictionary_Test(theDictionary.D) == true)
{
Dictionary_Find(Account_String, rowindex, theDictionary.D, Cash_Value, txtCurrency.Text); //Method that makes use of the dictionary values, above dictionary checks only if it exists
}
如果使用 A-T 的字典,那将是很多 if/else 的!有一个更好的方法吗?我发现一个线程提到了同样的主题,通过将字典添加到字典数组 [] 然后循环遍历它,但是如果找到匹配项,我如何获取匹配字典的名称或使用我的第二种方法 Dictionary_Find,使用匹配字典?
【问题讨论】:
-
为什么不只是一个字典列表,而不是dictionary_A-T?
标签: c# loops dictionary