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

前端之家收集整理的这篇文章主要介绍了c# – IEquatable接口检查null时要做什么前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_502_1@我已经使用以下代码在类中实现了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.f2er.com/csharp/243819.html

猜你在找的C#相关文章