You need to override GetHashCode()
whenever you override Equals()
(which you should also do when you implement IEquatable<T>
) so that classes that rely on it, such as Dictionary<TKey, TValue>
, can handle it correctly. It's one of those inner details that annoyingly leaks out. More information.
As for whether comparing your IDs is sufficient for determining equality, if you're absolutely sure that two entities with the same ID should be considered equivalent under all circumstances, then go for it. That's a question only the requirements of your application can answer. I've done it quite a few times myself, almost always with read-only entities.
I would implement it this way:
// IEquatable<MyClass> implementation
public bool Equals(MyClass other)
{
if (other == null)
{
return false;
}
return this.ID == other.ID;
}
// Override of default Object.Equals()
public override bool Equals(object other)
{
return this.Equals(other as MyClass);
}
public override int GetHashCode()
{
// Call ID.GetHashCode() because ID may not always be int,
// and the ID type might have a more useful implementation
// of GetHashCode() to offer.
return this.ID.GetHashCode();
}
You should probably also override the ==
and !=
operators in this case. You can always use Object.ReferenceEquals()
if you want to distinguish between two distinct references with the same ID.