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);
}

相关文章

在要实现单例模式的类当中添加如下代码:实例化的时候:frmC...
1、如果制作圆角窗体,窗体先继承DOTNETBAR的:public parti...
根据网上资料,自己很粗略的实现了一个winform搜索提示,但是...
近期在做DSOFramer这个控件,打算自己弄一个自定义控件来封装...
今天玩了一把WMI,查询了一下电脑的硬件信息,感觉很多代码都...
最近在研究WinWordControl这个控件,因为上级要求在系统里,...