比如说Point2类,以及以下等于:
public override bool Equals ( object obj ) public bool Equals ( Point2 obj )
这是有效C#3中显示的那个:
public override bool Equals ( object obj ) { // STEP 1: Check for null if ( obj == null ) { return false; } // STEP 3: equivalent data types if ( this.GetType ( ) != obj.GetType ( ) ) { return false; } return Equals ( ( Point2 ) obj ); } public bool Equals ( Point2 obj ) { // STEP 1: Check for null if nullable (e.g., a reference type) if ( obj == null ) { return false; } // STEP 2: Check for ReferenceEquals if this is a reference type if ( ReferenceEquals ( this, obj ) ) { return true; } // STEP 4: Possibly check for equivalent hash codes if ( this.GetHashCode ( ) != obj.GetHashCode ( ) ) { return false; } // STEP 5: Check base.Equals if base overrides Equals() System.Diagnostics.Debug.Assert ( base.GetType ( ) != typeof ( object ) ); if ( !base.Equals ( obj ) ) { return false; } // STEP 6: Compare identifying fields for equality. return ( ( this.X.Equals ( obj.X ) ) && ( this.Y.Equals ( obj.Y ) ) ); }
Daniel LeChe.. 36
在接受obj的那个中,如果obj的类型是Point2,则调用特定类型的Equals.在特定类型的Equals中,确保所有成员具有相同的值.
public override bool Equals ( object obj ) { return Equals(obj as Point2); } public bool Equals ( Point2 obj ) { return obj != null && obj.X == this.X && obj.Y == this.Y ... // Or whatever you think qualifies as the objects being equal. }
您可能应该重写GetHashCode以确保"相等"的对象具有相同的哈希码.
在接受obj的那个中,如果obj的类型是Point2,则调用特定类型的Equals.在特定类型的Equals中,确保所有成员具有相同的值.
public override bool Equals ( object obj ) { return Equals(obj as Point2); } public bool Equals ( Point2 obj ) { return obj != null && obj.X == this.X && obj.Y == this.Y ... // Or whatever you think qualifies as the objects being equal. }
您可能应该重写GetHashCode以确保"相等"的对象具有相同的哈希码.
MSDN上也有一整套指南.你应该好好读一读,这既棘手又重要.
我发现最有帮助的几点:
值类型没有标识,所以在a中struct Point
你通常会按成员比较来做成员.
引用类型通常具有标识,因此Equals测试通常在ReferenceEquals处停止(默认情况下,无需覆盖).但是有一些例外,例如string和your class Point2
,其中一个对象没有有用的标识,然后你覆盖Equality成员来提供你自己的语义.在这种情况下,请遵循指南以首先完成null和其他类型的情况.
而且有充分的理由保持GethashCode()
和operator==
同步为好.
我用过的技术对我有用,如下所示.注意,我只是基于单个属性(Id)而不是两个值进行比较.根据需要调整
using System; namespace MyNameSpace { public class DomainEntity { public virtual int Id { get; set; } public override bool Equals(object other) { return Equals(other as DomainEntity); } public virtual bool Equals(DomainEntity other) { if (other == null) { return false; } if (object.ReferenceEquals(this, other)) { return true; } return this.Id == other.Id; } public override int GetHashCode() { return this.Id; } public static bool operator ==(DomainEntity item1, DomainEntity item2) { if (object.ReferenceEquals(item1, item2)) { return true; } if ((object)item1 == null || (object)item2 == null) { return false; } return item1.Id == item2.Id; } public static bool operator !=(DomainEntity item1, DomainEntity item2) { return !(item1 == item2); } } }