I have POCOs from a SQL Server database that have an identity ID field. I would like to implement IEquatable so I can check if they're the same record, use .Contains()
on List<T>
etc.
Assuming I will never need to compare unsaved instances, is it sufficient to just implement using:
public bool Equals(MyClass other)
{
return this.ID == other.ID;
}
public override int GetHashCode()
{
return this.ID;
}
I'm about to do this, but just wanted to check that's fine, as I'm not 100% sure what GetHashCode has to do with it, or if there are any other considerations (such as the "unsaved instance" one, which I can discard) that I'm not aware of.