字典键不包含已包含在键中的键

发布于 2024-08-30 08:12:17 字数 938 浏览 2 评论 0原文

为什么以下“存在”布尔变量的值为 false???

foreach (Cell existCell in this.decoratorByCell.Keys)
{   
            //this call yield the same hashcode for both cells. still exist==false
            bool exist =
                this.decoratorByCell.ContainsKey(existCell);
}

我已经重写了 GetHashCode() & equals() 方法如下:

public override int GetHashCode()
{
            string nodePath = GetNodePath();

            return nodePath.GetHashCode() + m_ownerColumn.GetHashCode();
}

public bool Equals(Cell other)
{
bool nodesEqual = (other.OwnerNode == null && this.OwnerNode == null) || (other.GetNodePath() == this.GetNodePath());
bool columnsEqual = (other.OwnerColumn == null && this.OwnerColumn == null) || (other.OwnerColumn == this.OwnerColumn);
bool treesEqual = (this.m_ownerTree == other.m_ownerTree);

return (nodesEqual && columnsEqual && treesEqual);
}

Why is the following 'exist' boolean variable getting a value of false???

foreach (Cell existCell in this.decoratorByCell.Keys)
{   
            //this call yield the same hashcode for both cells. still exist==false
            bool exist =
                this.decoratorByCell.ContainsKey(existCell);
}

I've overridden GetHashCode() & Equals() Methods as follows:

public override int GetHashCode()
{
            string nodePath = GetNodePath();

            return nodePath.GetHashCode() + m_ownerColumn.GetHashCode();
}

public bool Equals(Cell other)
{
bool nodesEqual = (other.OwnerNode == null && this.OwnerNode == null) || (other.GetNodePath() == this.GetNodePath());
bool columnsEqual = (other.OwnerColumn == null && this.OwnerColumn == null) || (other.OwnerColumn == this.OwnerColumn);
bool treesEqual = (this.m_ownerTree == other.m_ownerTree);

return (nodesEqual && columnsEqual && treesEqual);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

明媚殇 2024-09-06 08:12:17

您的 EqualsGetHashCode 实现做了非常不同的事情。他们应该互相镜像。

您在 GetHashCode 中没有提及您在 Equals 实现中使用的 m_ownerTree

此外,将哈希码相加并不是计算哈希值的最简单方法。您可能需要对它们进行异或 (^)。

Your Equals and GetHashCode implementations do very different things. They should be mirroring each other.

You have no mention in GetHashCode to the m_ownerTree that you are using in your Equals implementation.

Also, adding up hashcodes is not the bast way of computing a hash. You may want to xor them (^) up.

空心空情空意 2024-09-06 08:12:17

哈希算法必须具有以下属性:

  • 如果两个事物相等,那么它们具有相同的哈希哈希

算法应该具有以下属性:

  • 更改可变对象不会改变它的散列码
  • 快,
  • 永远不会抛出异常
  • 对象之间的微小差异应该会导致散列码的大差异(理想情况下是 50%)

您的散列算法是否具有第一个必要的属性?在我看来,它并不像它那样。

A hash algorithm must have the following property:

  • if two things are equal then they have the same hash

A hash algorithm should have the following properties:

  • changing a mutable object does not change its hash code
  • fast
  • never throw an exception
  • small differences between objects should cause large (ideally 50% of the bits) differences in hash code

Does your hash algorithm have the first, necessary property? It doesn't look to me like it does.

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