c#等于,但不管订单如何

发布于 2025-01-21 04:26:13 字数 819 浏览 0 评论 0原文

我是一个班级边缘,看起来像遵循

class Edge 
{
    public Node left { get; private set; }
    public Node right { get; private set; }
    public int Costs { get; private set; }
}

我想编写一个等式,无论订单如何,它都会返回真实。因此,当左边是“巴黎”,右为“ rom”时,我想返回true,如果它与右边的边缘进行比较,而右边是“巴黎”,而左则是“ rom”。

“ ROM”和“ PARIS”是节点类的名称。节点类只有一个名称的字段。

我试图写下自己的平等

  public override bool Equals(object obj)
    {
        return obj is Edge edge &&
               (EqualityComparer<Node>.Default.Equals(left, edge.left) || EqualityComparer<Node>.Default.Equals(right, edge.left)) &&
               (EqualityComparer<Node>.Default.Equals(right, edge.right) || EqualityComparer<Node>.Default.Equals(right, edge.left));
    }

,然后将边缘添加到边缘标题。

我对应该尝试的事情一无所知。

I hava a class Edge which looks like following

class Edge 
{
    public Node left { get; private set; }
    public Node right { get; private set; }
    public int Costs { get; private set; }
}

I want to write an equals which returns true regardless of the order. So when left is 'Paris' and right is 'Rom' I want to return true if it compares with an Edge where right is 'Paris' and left is 'Rom'.

'Rom' and 'Paris' are the names of the Node class. The node class just has a field for the name.

I tried to write my own equals like that

  public override bool Equals(object obj)
    {
        return obj is Edge edge &&
               (EqualityComparer<Node>.Default.Equals(left, edge.left) || EqualityComparer<Node>.Default.Equals(right, edge.left)) &&
               (EqualityComparer<Node>.Default.Equals(right, edge.right) || EqualityComparer<Node>.Default.Equals(right, edge.left));
    }

And I add edges to a HashSet of Edges.

I am little bit clueless about what I should try.

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

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

发布评论

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

评论(1

旧情勿念 2025-01-28 04:26:13

您的代码检查一个边的是否在右边缘的中的 是否一个边的值在字段中。这可能会导致以下错误的equals()检查:

edge 1: (42, 50)
            / |
           /  |   
          /   |
edge 2: (50, 50)

在这种情况下,您会找到一个“交叉”链接和一个“直线”链接,具体取决于您在哪里开始equals()< /代码>来自。但是检查应既应该是“交叉”或“笔直”。

您可以在equals()方法中更改检查,如下:

public sealed override bool Equals(object obj)
{
    return obj is Edge edge &&
           (
               (
                   // L1 == L2 && R1 == R2
                   EqualityComparer<Node>.Default.Equals(Left, edge.Left) &&
                   EqualityComparer<Node>.Default.Equals(Right, edge.Right)
               ) ||
               (
                   // L1 == R2 && R1 == L2
                   EqualityComparer<Node>.Default.Equals(Left, edge.Right) &&
                   EqualityComparer<Node>.Default.Equals(Right, edge.Left)
               )
           );
}

第一个块检查一个是否转到另一个和一个转到另一个。第二个块检查“交叉”部分,其中一个转到另一个,一个右转到另一个

You code checks if the left value of one edge is in either the left or right field of the other edge AND if the right value of one edge is in either the left or right field. This can lead to the following wrong Equals() checks:

edge 1: (42, 50)
            / |
           /  |   
          /   |
edge 2: (50, 50)

In this case you will find one "cross" link and one "straight" link, depending on where you start your Equals() call from. But the checks should be either both "cross" or both "straight".

You can change the checks in the Equals() method as follow:

public sealed override bool Equals(object obj)
{
    return obj is Edge edge &&
           (
               (
                   // L1 == L2 && R1 == R2
                   EqualityComparer<Node>.Default.Equals(Left, edge.Left) &&
                   EqualityComparer<Node>.Default.Equals(Right, edge.Right)
               ) ||
               (
                   // L1 == R2 && R1 == L2
                   EqualityComparer<Node>.Default.Equals(Left, edge.Right) &&
                   EqualityComparer<Node>.Default.Equals(Right, edge.Left)
               )
           );
}

The first block checks if the one Left goes to the other Left and the one Right goes to the other Right. The second block checks the "crossed" part, where one Left goes to the other Right and the one Right goes to the other Left.

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