2012-02-16 12 views
65

ओवरराइड करने का सही तरीका मैंने पहले कभी ऐसा नहीं किया है, इसलिए मैं उम्मीद कर रहा था कि कोई मुझे मेरी कक्षा के लिए छोड़कर() और GetHashCode() के ओवरराइड को लागू करने का सही दिखा सकता है।बराबर() और 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 पर एक नज़र ले लिया है, लेकिन मैं उम्मीद कर रही थी किसी ने मुझे मेरे अपने उदाहरण के भीतर दिखा सकता है।

किसी भी मदद की सराहना की जाएगी।

इस तरह अपनी कक्षा पर GetHashCode() धन्यवाद

+0

उस पृष्ठ पर जो आपने लिंक किया था: "गैर-अपरिवर्तनीय प्रकारों में ऑपरेटर == को ओवरराइड करना अच्छा नहीं है।" कार्य() काम करने के लिए अन्य और बेहतर तरीके हैं। –

+0

@ हेनक होल्टरमैन समानता ऑपरेटर ओवरराइडिंग == की अनुशंसा नहीं की जाती है; ओवरराइडिंग बराबर की सिफारिश नहीं की जाती है। –

+0

@ सौहाइबबेस - यह (बहुत दृढ़ता से) सिंक में '==' और 'बराबर()' रखने के लिए अनुशंसित है। –

उत्तर

67

आप ओवरराइड कर सकते हैं बराबर() और: जब अधिभावी में समानता के लिए अपने परीक्षण के रूप में एक प्राथमिक कुंजी का उपयोग कर

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 <> ?: पब्लिक क्लास अनुशंसा डीटीओ लागू करने की आवश्यकता नहीं है: Iquatable ... जब मुझे कोई त्रुटि मिलती है: DataTransferObjects.RecommendationDTO इंटरफ़ेस सदस्य सिस्टम को लागू नहीं करता है। Iquatable .quals (DataTransferObjects.RecommendationDTO) – Nugs

+0

यह एक बहुत ही बुनियादी समाधान है जो विशेष रूप से हैश कोड पीढ़ी के साथ और संबंधित == और! = ऑपरेटरों को ओवरराइड करने के साथ सर्वोत्तम प्रथाओं को संबोधित नहीं करता है। – 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

सावधान रहें बराबर() क्योंकि यह ऑब्जेक्ट जारी रखने के बाद ही काम करता है। इससे पहले कि आपके ऑब्जेक्ट्स में अभी तक प्राथमिक कुंजी नहीं हैं और स्मृति में मौजूद लोगों की आईडी सभी शून्य हैं।

मैं आधार का उपयोग करता हूं। एक्वाल्स() यदि ऑब्जेक्ट आईडी में से कोई भी शून्य है लेकिन संभवतः एक और मजबूत तरीका है।