【发布时间】:2020-06-18 05:32:41
【问题描述】:
声明
private Dictionary<Tuple<String, int>, int> securityLevel;
在构造函数内部进行初始化
securityLevel.Add(Tuple.Create("Agent", 0), 1);
securityLevel.Add(Tuple.Create("DoubleAgent", 500), 2);
securityLevel.Add(Tuple.Create("FieldOfficeManager", 2105086), 3);
securityLevel.Add(Tuple.Create("DivisionChief", 8388350), 4);
securityLevel.Add(Tuple.Create("RegionalManager", 2104960), 5);
securityLevel.Add(Tuple.Create("ChiefInformationSecurityOfficer", 6307582), 6);
我的方法
private int getLowestSecurityLevel(Dictionary<Tuple, int> entry)
{
int currentSecurity = 0;
// not sure how to iterate a Dictionary with a Tuple? I want to get the title of the Geek Squad agent
foreach (KeyValuePair<Tuple<String, int>, int> item in entry)
{
}
}
Visual Studio 告诉我
无法转换 'System.Collections.Generic.KeyValuePair, int>' 到 'System.Collections.Generic.KeyValuePair'
【问题讨论】:
-
“我想获得 Geek Squad 特工的头衔”- 来自什么?从
currentSecurity = 0匹配元组中的 int,即您想要“代理”?如果这是您需要匹配的值,为什么不是 int 字典的键而不是整个元组?字典值中的 1-6 代表什么? -
@Rup 我更新了问题以包括我如何声明它并提供有关它如何初始化的信息
-
好的,但这不是我问的。
-
currentSecurity值如何属于Tuple<String, int>, int? -
那么逻辑是如果传递的字典不包含任何条目,那么我们返回 0,否则我们将“currentSecurity”设置为最低的 int。但是它的使用方式并不能帮助我迭代字典
标签: c# dictionary tuples