自定义类用作字典中的键但未找到键

发布于 2024-08-08 16:01:20 字数 955 浏览 3 评论 0原文

我有一个类,如下所示,用作 Dictionary 中的键 我在尝试在这本字典中查找任何键时遇到问题,它根本找不到它。如您所见,我已经重写了 EqualsGetHashCode

查找我正在使用的密钥

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

作死小能手 2024-08-15 16:01:20

您尚未覆盖 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.

清旖 2024-08-15 16:01:20

我遇到了这个问题,事实证明字典正在比较我的键的引用,而不是对象中的值。

我使用自定义 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.

末骤雨初歇 2024-08-15 16:01:20

看起来您正在比较两个字符串。 Iirc,当使用 .Equals() 时,您正在比较字符串的引用,而不是实际内容。要实现使用字符串的 EqualityComparer,您需要使用 String.Compare() 方法。

public class EqualityComparer : IEqualityComparer<ValuesAandB>
{
     public bool Equals(ValuesAandB x, ValuesAandB y)
     {
          return ((String.Compare(x.valueA,y.valueA) == 0) &&
            (String.Compare(x.valueB, y.valueB) == 0));
     }
     // gethashcode stuff here
}

我的代码可能有点不对劲,这应该会让你接近......

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.

public class EqualityComparer : IEqualityComparer<ValuesAandB>
{
     public bool Equals(ValuesAandB x, ValuesAandB y)
     {
          return ((String.Compare(x.valueA,y.valueA) == 0) &&
            (String.Compare(x.valueB, y.valueB) == 0));
     }
     // gethashcode stuff here
}

I could be a little off with the code, that should get you close...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文