【发布时间】:2015-12-05 09:18:14
【问题描述】:
这是我的代码:
public class MyKeyType
{
public int x;
public string operationStr;
}
private static Dictionary<MyKeyType, List<int>> m_Dict = new Dictionary<MyKeyType, List<int>>
{
{ new MyKeyType { x = MyValsType.File, operationStr = "LINK" }, new List<int> { 1,2,3,4,5 } },
{ new MyKeyType { x = MyValsType.File, operationStr = "COPY" }, new List<int> { 10,20,30,40,50 } },
.....
}
List<int> GetValList( int i, string op)
{
// The following line causes error:
return ( m_Dict [ new MyKeyType { x = i, operationStr = op } ] );
}
但是当我调用时出现错误“字典中不存在给定的键”:
GetValList( MyValsType.File, "LINK");
你能说出原因吗?非常感谢。
【问题讨论】:
-
您将新对象传递给字典,因此它不存在。
-
为什么?因为字典中不存在给定的键。我猜您想找到其属性(
x和operationStr)与上一个条目匹配的键,但这并不意味着该条目是相同的。例如:MyKeyType test = new MyKeyType { x = 1, operationStr = "1" }; m_Dict.Add(test, new List<int>() { 1, 2, 3 });如果你现在做MyKeyType test0 = new MyKeyType {x = 1, operationStr = "1" }; List<int> test2 = m_Dict[test0];你会得到一个错误。顺便说一句,下次你应该试着做一个小努力清楚地问清楚,例如像这样一个简单的例子。 -
@MaximGoncharuk 建议明确地重新定义给定变量很少是一个好建议。你不知道为什么 OP 想要这个数据结构。
-
有多种选择可以完成您的工作。您可以查找与给定属性匹配的条目(通过常规循环或 LINQ);或者您可以重新定义您的类,以便立即支持某些比较(例如:stackoverflow.com/questions/4188013/…)。
-
您没有覆盖 Equals 和 GetHashCode,也没有为字典提供 IEqualityComparer,因此 MyKeyType 类型的新对象永远不会匹配字典中存储的对象。像这样暴露字段的类不是字典键的理想选择,顺便说一句,更改字段太容易使字典条目无法挽回地丢失。
标签: c# dictionary key