C#平等比较失败

发布于 2025-01-24 11:30:13 字数 1640 浏览 0 评论 0原文

通过iquapable<>在以下类中,我在平等比较方面遇到了麻烦:

public class Bead: IEquatable<Bead>
{
    public string Name { get; set; }
    public Point2d Location { get; private set; }

    public void SetLocation(Point2d newLocation)
    {
        Location = newLocation;
    }

    #region equality comparison
    /// <summary>
    /// Equality comparisons
    /// </summary>
    /// <param name="other"></param>
    /// <returns></returns>
    public override bool Equals(object other)
    {
        if (!(other is Bead)) return false;
        return Equals((Bead)other); // Calls method below
    }
    public bool Equals(Bead other) // Implements IEquatable<Point2d>
    {
        return Location == other.Location && Name == other.Name;
    }
    public override int GetHashCode()
    {
        return this.Location.GetHashCode() * 67 + Name.GetHashCode(); // 67 = some prime number
    }
    public static bool operator ==(Bead a1, Bead a2)
    {
        if (a1 == null && a2 == null) return true;
        if (a1 == null || a2 == null) return false;
        return a1.Equals(a2);
    }
    public static bool operator !=(Bead a1, Bead a2)
    {
        if (a1 == null || a2 == null) return true;
        if (a1 == null && a2 == null) return false;
        return !a1.Equals(a2);
    } 
    #endregion
}

polymermotionsimulation.exe

中发生了一个未手动的'system.stackoverflowException'的例外。

我该如何解决?

I am having trouble with the equality comparison via IEquatable<> in the following class I wrote:

public class Bead: IEquatable<Bead>
{
    public string Name { get; set; }
    public Point2d Location { get; private set; }

    public void SetLocation(Point2d newLocation)
    {
        Location = newLocation;
    }

    #region equality comparison
    /// <summary>
    /// Equality comparisons
    /// </summary>
    /// <param name="other"></param>
    /// <returns></returns>
    public override bool Equals(object other)
    {
        if (!(other is Bead)) return false;
        return Equals((Bead)other); // Calls method below
    }
    public bool Equals(Bead other) // Implements IEquatable<Point2d>
    {
        return Location == other.Location && Name == other.Name;
    }
    public override int GetHashCode()
    {
        return this.Location.GetHashCode() * 67 + Name.GetHashCode(); // 67 = some prime number
    }
    public static bool operator ==(Bead a1, Bead a2)
    {
        if (a1 == null && a2 == null) return true;
        if (a1 == null || a2 == null) return false;
        return a1.Equals(a2);
    }
    public static bool operator !=(Bead a1, Bead a2)
    {
        if (a1 == null || a2 == null) return true;
        if (a1 == null && a2 == null) return false;
        return !a1.Equals(a2);
    } 
    #endregion
}

An unhandled exception of type 'System.StackOverflowException' occurred in PolymerMotionSimulation.exe

enter image description here

How can I solve this?

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

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

发布评论

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

评论(1

半夏半凉 2025-01-31 11:30:13

您的==操作员会自调用,导致无限递归。要检查对象是否为null引用,更好地使用是null ReferenceEquals(a1,null)

public static bool operator ==(Bead a1, Bead a2)
{
    if (a1 is null && a2 is null) return true;
    if (a1 is null || a2 is null) return false;
    return a1.Equals(a2);
}

!=的Similary。

或者:

public static bool operator ==(Bead a1, Bead a2)
{
    if (ReferenceEquals(a1, null) && ReferenceEquals(a2, null)) return true;
    if (ReferenceEquals(a1, null) || ReferenceEquals(a2, null)) return false;
    return a1.Equals(a2);
}

Your == operator calls itself, leading to infinite recursion. To check if an object is a null reference, better use is null or ReferenceEquals(a1, null):

public static bool operator ==(Bead a1, Bead a2)
{
    if (a1 is null && a2 is null) return true;
    if (a1 is null || a2 is null) return false;
    return a1.Equals(a2);
}

and similary for !=.

Or:

public static bool operator ==(Bead a1, Bead a2)
{
    if (ReferenceEquals(a1, null) && ReferenceEquals(a2, null)) return true;
    if (ReferenceEquals(a1, null) || ReferenceEquals(a2, null)) return false;
    return a1.Equals(a2);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文