-1

I can not understand what the performance of the method Equals() and GetHashCode(). At what time I can use one or the other, or under what conditions.

I can't seem to find examples on how to implement them in the context of NHibernate.

BrennQuin
  • 656
  • 10
  • 19
  • 2
    This may shed some light: http://stackoverflow.com/questions/371328/why-is-it-important-to-override-gethashcode-when-equals-method-is-overridden/371348#371348 – MikeG Jan 15 '15 at 22:38
  • 1
    Brenn, sadly, the topic of recommended tutorials is off-topic here on SO, so this question will likely be closed. I suggest you do some background reading on hash tables to understand the purpose of GetHashCode. http://en.wikipedia.org/wiki/Hash_table – spender Jan 15 '15 at 22:38
  • I had to try, thanks for the recommendations, I get them immediately. – BrennQuin Jan 15 '15 at 22:48

1 Answers1

-1

because there is a tag NHibernate the OP propably wants NHibernate specific. so here is a base class for persisted entities which implements Equals() and GetHashcCode()

public abstract class Entity
{
    public virtual long Id { get; protected set; }

    public override bool Equals(object obj)
    {
        var other = obj as Entity;
        return other != null && (Id == 0 ? ReferenceEquals(this, other) : Id == other.Id);
    }

    public override int GetHashCode()
    {
        return Id.GetHashCode();
    }
}
Firo
  • 30,626
  • 4
  • 55
  • 94