微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – IEquatable接口检查null时要做什么

我已经使用以下代码在类中实现了IEquatable接口.
public bool Equals(ClauseBE other)
        {
            if (this._id == other._id)
            {
                return true;
            }
            return false;
        }

        public override bool Equals(Object obj)
        {
            if (obj == null)
            {
                return base.Equals(obj);
            }

            if (!(obj is ClauseBE))
            {
                throw new InvalidCastException("The 'obj' argument is not a ClauseBE object.");
            }

            return Equals(obj as ClauseBE);
        }

        public override int GetHashCode()
        {
            return this._id.GetHashCode();
        }

        public static bool operator ==(ClauseBE a,ClauseBE b)
        {
            // cast to object so we call the overloaded Equals function which appropriately checks when b is null.
            return a.Equals(b as object);
        }

        public static bool operator !=(ClauseBE a,ClauseBE b)
        {
            // cast to object so we call the overloaded Equals function which appropriately checks when b is null.
            return !a.Equals(b as object);
        }

代码适用于大多数情况.但是,以下检查会在等于运算符重载方法中引发异常,因为a为null,因此没有Equals方法.

if(this.Clause != null)
{

}

解决此问题的标准方法是什么?

编辑

我已经去过这个,但看起来很麻烦.我希望有一种更优雅的方式来实现这一目标.

public static bool operator ==(ClauseBE a,ClauseBE b)
    {
        if (a as object == null && b as object == null)
        {
            return true;
        }

        if ((a as object == null && b as object != null)
            || (b as object == null && a as object != null))
        {
            return false;
        }

        // cast to object so we call the overloaded Equals function which appropriately checks when b is null.
        return a.Equals(b as object);
    }

    public static bool operator !=(ClauseBE a,ClauseBE b)
    {
        if (a as object == null && b as object == null)
        {
            return false;
        }

        if((a as object == null && b as object != null)
            || (b as object == null && a as object != null))
        {
            return true;
        }

        // cast to object so we call the overloaded Equals function which appropriately checks when b is null.
        return !a.Equals(b as object);
    }

谢谢大家.我从每个人那里得到了很多好的建议,我真的很感激.这是我最终确定的,它比我开始时更优雅.除运算符重载外,所有代码都相同.

public static bool operator ==(ClauseBE a,ClauseBE b)
{
    if (ReferenceEquals(a,null) && ReferenceEquals(b,null))
    {
        return true;
    }

    if (ReferenceEquals(a,null) || ReferenceEquals(b,null))
    {
        return false;
    }

    return a.Equals(b);
}

public static bool operator !=(ClauseBE a,ClauseBE b)
{
    return !(a == b);
}

解决方法

我总是发现使用null处理编写静态运算符更容易,并且使用Equals覆盖调用重载运算符,并将“this”作为参数之一.

Guidelines for Overloading Equals() and Operator == (C# Programming Guide)

//add this code to class ThreeDPoint as defined prevIoUsly
//
public static bool operator ==(ThreeDPoint a,ThreeDPoint b)
{
    // If both are null,or both are same instance,return true.
    if (System.Object.ReferenceEquals(a,b))
    {
        return true;
    }

    // If one is null,but not both,return false.
    if (((object)a == null) || ((object)b == null))
    {
        return false;
    }

    // Return true if the fields match:
    return a.x == b.x && a.y == b.y && a.z == b.z;
}

public static bool operator !=(ThreeDPoint a,ThreeDPoint b)
{
    return !(a == b);
}

原文地址:https://www.jb51.cc/csharp/243819.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐