自定义类用作字典中的键但未找到键
我有一个类,如下所示,用作 Dictionary
中的键 我在尝试在这本字典中查找任何键时遇到问题,它根本找不到它。如您所见,我已经重写了 Equals
和 GetHashCode
。
查找我正在使用的密钥
ValuesAandB key = new ValuesAandB(A,B);
if (DictionaryName.ContainsKey(key)) {
...
}
还有其他我丢失的东西吗?谁能指出我做错了什么?
private class ValuesAandB {
public string valueA;
public string valueB;
// Constructor
public ValuesAandB (string valueAIn, string valueBIn) {
valueA = valueAIn;
valueB = ValueBIn;
}
public class EqualityComparer : IEqualityComparer<ValuesAandB> {
public bool Equals(ValuesAandB x, ValuesAandB y) {
return ((x.valueA.Equals(y.valueA)) && (x.valueB.Equals(y.valueB)));
}
public int GetHashCode(ValuesAandB x) {
return x.valueA.GetHashCode() ^ x.valueB.GetHashCode();
}
}
}
在有人问之前,是的,这些值在字典中!
I have a class, show below, which is used as a key in a Dictionary<ValuesAandB, string>
I'm having issues when trying to find any key within this dictionary, it never finds it at all. As you can see, I have overridden Equals
and GetHashCode
.
To look for the key I'm using
ValuesAandB key = new ValuesAandB(A,B);
if (DictionaryName.ContainsKey(key)) {
...
}
Is there anything else that I'm missing? Can anyone point out what I'm doing wrong?
private class ValuesAandB {
public string valueA;
public string valueB;
// Constructor
public ValuesAandB (string valueAIn, string valueBIn) {
valueA = valueAIn;
valueB = ValueBIn;
}
public class EqualityComparer : IEqualityComparer<ValuesAandB> {
public bool Equals(ValuesAandB x, ValuesAandB y) {
return ((x.valueA.Equals(y.valueA)) && (x.valueB.Equals(y.valueB)));
}
public int GetHashCode(ValuesAandB x) {
return x.valueA.GetHashCode() ^ x.valueB.GetHashCode();
}
}
}
And before anyone asks, yes the values are in the Dictionary!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您尚未覆盖 Equals 和 GetHashCode。您已经实现了第二个类,它可以用作 EqualityComparer。如果不使用 EqualityComparer 构造字典,则不会使用它。
最简单的修复方法是直接覆盖 GetHashCode 和 Equals,而不是实现比较器(比较器通常仅在您需要提供多种不同的比较类型(例如区分大小写和不区分大小写)或需要能够执行比较时才有意义)在你无法控制的课程上。
You have not overridden Equals and GetHashCode. You have implemented a second class which can serve as an EqualityComparer. If you don't construct the Dictionary with the EqualityComparer, it will not be used.
The simplest fix would be to override GetHashCode and Equals directly rather than implementing a comparer (comparers are generally only interesting when you need to supply multiple different comparison types (case sensitive and case insensitive for example) or when you need to be able to perform comparisons on a class which you don't control.
我遇到了这个问题,事实证明字典正在比较我的键的引用,而不是对象中的值。
我使用自定义 Point 类作为键。我重写了 ToString() 和 GetHashCode() 方法,并且中提琴键查找工作正常。
I had this problem, turns out the dictionary was comparing referances for my key, not the values in the object.
I was using a custom Point class as keys. I overrode the ToString() and the GetHashCode() methods and viola, key lookup worked fine.
看起来您正在比较两个字符串。 Iirc,当使用 .Equals() 时,您正在比较字符串的引用,而不是实际内容。要实现使用字符串的 EqualityComparer,您需要使用 String.Compare() 方法。
我的代码可能有点不对劲,这应该会让你接近......
It looks like you're comparing two strings. Iirc, when using .Equals(), you are comparing the reference of the strings, not the actual contents. To implement an EqualityComparer that works with strings, you would want to use the String.Compare() method.
I could be a little off with the code, that should get you close...