重载的运算符参数始终为 null

发布于 2024-09-26 01:48:58 字数 1088 浏览 3 评论 0原文

所以我有一个类,它重写 Equals(object obj) 和 GetHashCode() 以及实现 IEquatable。为了在检查相等性时使使用这种类型更加自然,我想,哎呀,我会重载相等运算符和不等运算符,不用担心......

呃哦,担心......考虑以下内容 - 其中两个 myType 实例不为空:

if (myType != container.myType) //NullReferenceException
{
    //never get here
}
//never get here either

现在,容器只是另一个类,用于保存 myType 的实例以及用于缓存项目的其他内容。

这是来自 myType 的实际(相关)代码:

public class MyType : IEquatable<MyType>
{
    public static bool operator ==(MyType myTypeA, MyType myTypeB)
    {
        return myTypeA.Equals(myTypeB);
    }

    public static bool operator !=(MyType myTypeA, MyType myTypeB)
    {
        return !(myTypeA == myTypeB);
    }

    public override bool Equals(object obj)
    {
        if (obj != null && obj is MyType)
        {
            return Equals((MyType)obj);
        }
        return false;
    }

    public bool Equals(MyType other)
    {
        if (other != null)
        {
            return other.ToString() == ToString();
        }
        return false;
    }
}

在这方面有任何经验吗?

谢谢。

So I have a class which overrides Equals(object obj) and GetHashCode() along with implementing IEquatable. To make working with this type a little more natural when checking for equality I thought, heck, I'd overload the equality operator and inequality operator, no worries...

Uh oh, worries... consider the following - where both myType instances are NOT null:

if (myType != container.myType) //NullReferenceException
{
    //never get here
}
//never get here either

Now, container is just another class to hold an instance of myType among other things which is used for caching items.

Here's the actual (relevant) code from myType:

public class MyType : IEquatable<MyType>
{
    public static bool operator ==(MyType myTypeA, MyType myTypeB)
    {
        return myTypeA.Equals(myTypeB);
    }

    public static bool operator !=(MyType myTypeA, MyType myTypeB)
    {
        return !(myTypeA == myTypeB);
    }

    public override bool Equals(object obj)
    {
        if (obj != null && obj is MyType)
        {
            return Equals((MyType)obj);
        }
        return false;
    }

    public bool Equals(MyType other)
    {
        if (other != null)
        {
            return other.ToString() == ToString();
        }
        return false;
    }
}

Any experience on this front?

Thanks.

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

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

发布评论

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

评论(3

开始看清了 2024-10-03 01:48:59

正如其他人所说,您需要仔细检查空值,因为它会再次调用您的相等函数,通常会导致 StackOverflowException。

当我在类上使用 IEquatable 接口时,我通常使用以下代码:

public override bool Equals(object obj)
{
    // If obj isn't MyType then 'as' will pass in null
    return this.Equals(obj as MyType);
}

public bool Equals(MyType other)
{
    if (object.ReferenceEquals(other, null))
    {
        return false;
    }

    // Actual comparison code here
    return other.ToString() == this.ToString();
}

As the others have stated you need to be carefull checking for nulls as it will call your equality function again, normally resulting in a StackOverflowException.

When I use the IEquatable interface on classes I normally use the following code:

public override bool Equals(object obj)
{
    // If obj isn't MyType then 'as' will pass in null
    return this.Equals(obj as MyType);
}

public bool Equals(MyType other)
{
    if (object.ReferenceEquals(other, null))
    {
        return false;
    }

    // Actual comparison code here
    return other.ToString() == this.ToString();
}
穿透光 2024-10-03 01:48:58

几个指针 -

  1. 如果您在类上重写了 ==!=,请确保使用 ReferenceEquals 检查重载内是否为 null实现而不是 ==,因为这会调用重载的运算符,然后进入循环或尝试在 null this 引用上调用 Equals ,这可能就是这里发生的情况。
  2. 不要重写类上的 ==!= 。这些运算符旨在实现值相等,而类并不是真正设计为具有值相等。删除运算符重载,或将 MyType 设为结构体。

Couple of pointers -

  1. If you've overridden == and != on classes, make sure to use ReferenceEquals to check for null inside the overload implementations rather than ==, as that will call your overloaded operator and either go into a loop or try to call Equals on a null this reference, which is probably what is happening here.
  2. Don't override == and != on classes. Those operators are meant for value equality, and classes aren't really designed to have value equality. Either remove the operator overloads, or make MyType a struct.
笑梦风尘 2024-10-03 01:48:58

棘手的一个...问题是您在 Equal 覆盖中使用相等运算符,如下所示:

public bool Equals(MyType other)
{
    if (other != null)

它转到您重载的 != 运算符,该运算符又转到您的 == 运算符,该运算符试图执行 null.Equals...

Tricky one... the problem is that you use the equality operator inside the Equal override as follows:

public bool Equals(MyType other)
{
    if (other != null)

It goes to your overloaded != operator, which in turn goes to your == operator, which trying to do null.Equals...

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