IEquatable接口零检查时,该怎么办 [英] IEquatable Interface what to do when checking for null

查看:98
本文介绍了IEquatable接口零检查时,该怎么办的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现在用下面的代码一类IEquatable接口。

 公共布尔等于(ClauseBE等)
{
如果(this._id == other._id)
{
返回真;
}
返回FALSE;
}

公众覆盖布尔等于(obj对象)
{
如果(OBJ == NULL)
{
返回base.Equals (OBJ);
}

如果((obj是ClauseBE)!)
{
抛出新InvalidCastException的(以下简称OBJ的说法是不是ClauseBE对象。);
}

返回等于(OBJ为ClauseBE);
}

公共覆盖INT的GetHashCode()
{
返回this._id.GetHashCode();
}

公共静态布尔运算符==(ClauseBE一,ClauseBE B)
{
//转换为对象,所以我们称之为重载等于功能,适当的检查当b为null。
返回a.Equals(B为对象);
}

公共静态布尔运算符!=(ClauseBE一,ClauseBE B)
{
//转换为对象,所以我们称之为重载等于功能,适当的检查当b为null。
返回a.Equals(B为对象)!;
}

此代码工作非常好于大多数的所有情况。但是,下面的检查抛出平等运算符重载方法的异常,因为一个为空,因此没有一个equals方法。

 如果(this.Clause!= NULL)
{

}

什么是解决这个问题的标准方式?



修改



我去了这一点,但它似乎很繁琐。我希望有做到这一点更优雅的方式。

 公共静态布尔运算符==(ClauseBE一,ClauseBE B) 
{
如果(一个为对象== NULL和放大器;和b为对象== NULL)
{
返回真;
}

如果((一个为对象== NULL和放大器;&安培;!b以对象= NULL)
||(二为对象== NULL和放大器;&安培; !作为一个对象= NULL))
{
返回FALSE;
}

//转换为对象,所以我们称之为重载等于功能时,b为null它适当地检查。
返回a.Equals(B为对象);
}

公共静态布尔运算符=(ClauseBE一,ClauseBE B)
{
如果(一个为对象== NULL和放大器;!和b的对象== NULL)
{
返回false;
}

如果((一个为对象== NULL和放大器;&安培;!b以对象= NULL)
||(二为对象== NULL和放大器;&安培; !作为一个对象= NULL))
{
返回真;
}

//转换为对象,所以我们称之为重载等于功能时,b为null它适当地检查。
返回a.Equals(B为对象)!;
}



解决方案



感谢所有。我得到了很多好的建议从每个人,我真的很感激。这是我最后决定,这是一个很多比我曾与开始更加优雅。所有的代码是除运算符重载一样的。

 公共静态布尔运算符==(ClauseBE一,ClauseBE B)
{
如果(的ReferenceEquals(一,空)及和放大器;的ReferenceEquals(b,NULL))
{
返回真;
}

如果(的ReferenceEquals(一,NULL)||的ReferenceEquals(B,NULL))
{
返回FALSE;
}

返回a.Equals(B);
}

公共静态布尔运算符=(ClauseBE一,ClauseBE B)
{
回报(A == B);!
}


解决方案

我总是发现它更容易编写与空处理静态的运营商,并拥有的Equals重写调用重载运算符这个作为参数之一。



从的指引重载equals()和运算符==(C#编程指南)

  //这个代码添加到类ThreeDPoint定义之前
//
公共静态布尔运算符==(ThreeDPoint一, ThreeDPoint b)
{
//如果都为null,或者两者都是相同的情况下,返回true。
如果(System.Object.ReferenceEquals(A,B))
{
返回真;
}

//如果有一个为空,但不同时,返回false。
如果(((对象)一== NULL)||((对象)b == NULL))
{
返回false;
}如果字段匹配

//返回true:
返回a.x == b.x&放大器;&安培; a.y == b.y&放大器;&安培; a.z == b.z;
}

公共静态布尔运算符=(ThreeDPoint一,ThreeDPoint B)
{
回报(A == B);!
}


I have implemented the IEquatable interface in a class with the following code.

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

This code work very well for most all cases. However, the following check throws an exception in the equality operator overload method because a is null and therefore does not have a Equals method.

if(this.Clause != null)
{

}

What is the standard way to solve this issue?

EDIT

I have gone to this, but it seems pretty cumbersome. I was hoping there was a more elegant way to accomplish this.

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

Solution

Thanks all. I got a lot of good tips from everyone, I really appreciate it. This is what I finally settled on, it's a lot more elegant than what I had started with. All code is the same except operator overloads.

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

解决方案

I've always found it easier to write the static operator with null handling, and have the Equals override call the overloaded operator with "this" as one of the parameters.

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

这篇关于IEquatable接口零检查时,该怎么办的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆