2012-02-16 7 views
65

これまでにこのようなことをしたことがないので、私のクラスでExcept()とGetHashCode()のオーバーライドを正しく実装できることを期待できました。Equals()とGetHashCode()をオーバーライドする正しい方法

私はLINQ Except()メソッドを使用できるようにクラスを変更しようとしています。

public class RecommendationDTO{public Guid RecommendationId { get; set; } 
public Guid ProfileId { get; set; } 
public Guid ReferenceId { get; set; } 
public int TypeId { get; set; } 
public IList<TagDTO> Tags { get; set; } 
public DateTime CreatedOn { get; set; } 
public DateTime? ModifiedOn { get; set; } 
public bool IsActive { get; set; } 
public object ReferencedObject { get; set; } 
public bool IsSystemRecommendation { get; set; } 
public int VisibilityScore { get; set; } 

public RecommendationDTO() 
{ 
} 

public RecommendationDTO(Guid recommendationid, 
          Guid profileid, 
          Guid referenceid, 
          int typeid, 
          IList<TagDTO> tags, 
          DateTime createdon, 
          DateTime modifiedon, 
          bool isactive, 
          object referencedobject) 
{ 
    RecommendationId = recommendationid; 
    ProfileId = profileid; 
    ReferenceId = referenceid; 
    TypeId = typeid; 
    Tags = tags; 
    CreatedOn = createdon; 
    ModifiedOn = modifiedon; 
    ReferencedObject = referencedobject; 
    IsActive = isactive; 
} 

public override bool Equals(System.Object obj) 
{ 
    // If parameter is null return false. 
    if (obj == null) 
    { 
     return false; 
    } 

    // If parameter cannot be cast to Point return false. 
    RecommendationDTO p = obj as RecommendationDTO; 
    if ((System.Object)p == null) 
    { 
     return false; 
    } 

    // Return true if the fields match: 
    return (ReferenceId == p.ReferenceId);// && (y == p.y); 
} 

public bool Equals(RecommendationDTO p) 
{ 
    // If parameter is null return false: 
    if ((object)p == null) 
    { 
     return false; 
    } 

    // Return true if the fields match: 
    return (ReferenceId == p.ReferenceId);// && (y == p.y); 
} 

//public override int GetHashCode() 
//{ 
// return ReferenceId;//^y; 
//}} 

私はhttp://msdn.microsoft.com/en-us/library/ms173147.aspxを見て撮影してきたが、私は誰かが私自身の例の中に私を見ることができ期待していました。

ご協力いただければ幸いです。このようなあなたのクラスの

あなたが(等号を上書きすることができ、あなたに

+0

リンク先のページで:「不変型では演算子==を上書きすることはお勧めできません。」 Except()を動作させるには他にも優れた方法があります。 –

+0

@Henk Holtermanをオーバーライドする等価演算子==は推奨されません。 Equalsをオーバーライドすることはお勧めできません。 –

+0

@SouhaiebBesbes - '=='と 'Equals()'を同期させておくことが強く推奨されています。 –

答えて

67

ありがとう)とGetHashCodeメソッド():オーバーライドに平等のためのテストとして、主キーを使用している場合

public override bool Equals(object obj) 
{ 
    var item = obj as RecommendationDTO; 

    if (item == null) 
    { 
     return false; 
    } 

    return this.RecommendationId.Equals(item.RecommendationId); 
} 

public override int GetHashCode() 
{ 
    return this.RecommendationId.GetHashCode(); 
} 
+0

IEquatableを実装する必要はありませんか?:public class RecommendationDTO:IEquatable ...エラーが発生した場合:DataTransferObjects.RecommendationDTOは、インターフェイスメンバーSystem.IEquatable を実装していません.Equals DataTransferObjects.RecommendationDTO) – Nugs

+0

これはベストプラクティス、特にハッシュコードの生成と関連する==および!=演算子のオーバーライドを扱わない非常に基本的なソリューションです。 objが現在のクラスから派生したクラスのインスタンスである可能性があるので、一般的なケースではチェックとして – LostNomad311

+0

が十分ではありません – ovolko

10
public override bool Equals(System.Object obj) 
{ 
    // Check if the object is a RecommendationDTO. 
    // The initial null check is unnecessary as the cast will result in null 
    // if obj is null to start with. 
    var recommendationDTO = obj as RecommendationDTO; 

    if (recommendationDTO == null) 
    { 
     // If it is null then it is not equal to this instance. 
     return false; 
    } 

    // Instances are considered equal if the ReferenceId matches. 
    return this.ReferenceId == recommendationDTO.ReferenceId; 
} 

public override int GetHashCode() 
{ 
    // Returning the hashcode of the Guid used for the reference id will be 
    // sufficient and would only cause a problem if RecommendationDTO objects 
    // were stored in a non-generic hash set along side other guid instances 
    // which is very unlikely! 
    return this.ReferenceId.GetHashCode(); 
} 
7

は注意してくださいオブジェクトが永続化された後にのみ機能するため、Equals()これに先立って、オブジェクトには主キーがまだなく、メモリ内のIDはすべてゼロです。

いずれかのオブジェクトIDがゼロの場合はbase.Equals()を使用しますが、おそらくより堅牢な方法があります。

関連する問題