Generally speaking, if you're overriding Equals, you want to override GetHashCode. The reason for this is because both are used to compare equality of your class/struct.
Equals is used when checking
Foo A, B;
if (A == B)
Since we know the pointer isn't likely to match, we can compare the internal members.
Equals(obj o)
{
if (o == null) return false;
MyType Foo = o as MyType;
if (Foo == null) return false;
if (Foo.Prop1 != this.Prop1) return false;
return Foo.Prop2 == this.Prop2;
}
GetHashCode is generally used by hash tables. The hashcode generated by your class should always be the same for a classes give state.
I typically do,
GetHashCode()
{
int HashCode = this.GetType().ToString().GetHashCode();
HashCode ^= this.Prop1.GetHashCode();
etc.
return HashCode;
}
Some will say that the hashcode should only be calculated once per object lifetime, but I don't agree with that (and I'm probably wrong).
Using the default implementation provided by object, unless you have the same reference to one of your classes, they will not be equal to each other. By overriding Equals and GetHashCode, you can report equality based on internal values rather than the objects reference.