Correct way to override Equals() and GetHashCode() [duplicate]

You can override Equals() and GetHashCode() on your class like this:

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

Leave a Comment