为了账号安全,请及时绑定邮箱和手机立即绑定

正确的方法来重写Equals()和GetHashCode()

正确的方法来重写Equals()和GetHashCode()

C#
婷婷同学_ 2019-08-27 17:40:08
正确的方法来重写Equals()和GetHashCode()我之前从未真正这样做过,所以我希望有人可以告诉我正确实现我的类的Except()和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:我希望有人可以在我自己的例子中向我展示。任何帮助,将不胜感激。谢谢
查看完整描述

3 回答

?
暮色呼如

TA贡献1853条经验 获得超9个赞

您可以在类上覆盖Equals()和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();}


查看完整回答
反对 回复 2019-08-27
?
肥皂起泡泡

TA贡献1829条经验 获得超6个赞

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();}


查看完整回答
反对 回复 2019-08-27
  • 3 回答
  • 0 关注
  • 583 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信