比如说Point2类,以及以下等于:
public override bool Equals ( object obj )
public bool Equals ( Point2 obj )
Run Code Online (Sandbox Code Playgroud)
这是有效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 ) ) );
}
Run Code Online (Sandbox Code Playgroud)
Dan*_*ant 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.
}
Run Code Online (Sandbox Code Playgroud)
您可能应该重写GetHashCode以确保"相等"的对象具有相同的哈希码.
Hen*_*man 27
MSDN上也有一整套指南.你应该好好读一读,这既棘手又重要.
我发现最有帮助的几点:
值类型没有标识,所以在a中struct Point你通常会按成员比较来做成员.
引用类型通常具有标识,因此Equals测试通常在ReferenceEquals处停止(默认情况下,无需覆盖).但是有一些例外,例如string和your class Point2,其中一个对象没有有用的标识,然后你覆盖Equality成员来提供你自己的语义.在这种情况下,请遵循指南以首先完成null和其他类型的情况.
而且有充分的理由保持GethashCode()和operator==同步为好.
use*_*543 10
我用过的技术对我有用,如下所示.注意,我只是基于单个属性(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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
小智 5
使用 C# 7 和is type varname模式匹配语法提供了一种使用以下任一方法Equals(object)处理null和类型检查的清理:
// using strongly-typed overload of Equals
public override bool Equals(object obj) => (obj is Point2 other) && Equals(other);
public bool Equals(Point2 other);
Run Code Online (Sandbox Code Playgroud)
// using the == operator (requires != to also be defined)
public override bool Equals(object obj) => (obj is Point2 other) && this == other;
public static bool operator ==(Point2 lhs, Point2 rhs);
public static bool operator !=(Point2 lhs, Point2 rhs);
Run Code Online (Sandbox Code Playgroud)
(显然您需要提供相关方法存根的实现。)
| 归档时间: |
|
| 查看次数: |
46423 次 |
| 最近记录: |