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

c# – IEquatable接口检查null时要做什么,第1张

概述我已经使用以下代码在类中实现了IEquatable接口. public bool Equals(ClauseBE other) { if (this._id == other._id) { return true; } return false; 我已经使用以下代码在类中实现了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);}
总结

以上是内存溢出为你收集整理的c# – IEquatable接口检查null时要做什么全部内容,希望文章能够帮你解决c# – IEquatable接口检查null时要做什么所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1249862.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-07
下一篇2022-06-07

发表评论

登录后才能评论

评论列表(0条)

    保存