Comparing two objects .

Implement the IEquatable interface. This defines a generalized method that a value type or class implements to create a type-specific method for determining equality of instances. Don't forget to override Equals(object) as well. More information here:

http://msdn.microsoft.com/en-us/library/ms131187.aspx


I think the answer is highly problem dependent. For example, you may want to consider objects equal only if all of their properties are equivalent. This would perhaps be the case where each object doesn't have a uniquely identifying property. If there is such a property (or properties), say an ID or ID and Version, that uniquely identifies each object of the type, then you may only want to compare based on that property (or properties).

The base pattern, however, ought to be something like:

if their references are equal (includes both null)
   return true
else if one object is null
   return false
else
   return value based on relevant properties

Note that if you override the Equals operator, you'll also want to override GetHashCode() so that the hash codes for equivalent objects are the same. This will ensure that data structures that use the hash code for determining duplicate keys work properly when the object is used as a key.