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

本文关键字:方法 Equals 重写 GetHashCode | 更新日期: 2023-09-27 18:24:49

我以前从未真正这样做过,所以我希望有人能向我展示为我的类实现Except()和GetHashCode()重写的正确方法。

我正在尝试修改这个类,以便可以使用LINQException()方法。

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但我希望有人能以我自己为榜样。

如有任何帮助,我们将不胜感激。

感谢

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

您可以在类上重写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();
}
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();
}

在重写Equals()时使用主键作为相等性测试时要小心,因为它只有在对象持久化后才能工作。在此之前,您的对象还没有主键,并且内存中对象的ID都为零。

我使用基地。Equals(),如果其中一个对象ID为零,但可能有更稳健的方法。