I have to do value comparisons of every field
If that is your final need, no computed value can avoid to compare single fields, unless that computed value is unique for any combination of field values, like (as an example) a string obtained concatenating the values of all the field of each object, but remember that converting to string some values can cause approximation and thus lead to wrong mismatch between objects (especially with floating point numbers).
Field by field comparison is the most accurate one can need, whereas hash/checksum computation is not meant for accurate comparison, but only for indexing, or as preliminary check to avoid more intensive computations (like yours), or other goals where field by field is not required.
You can eventually write a readonly property that compute the value once, only when needed, and store it as an hidden field, like:
public class _Object
{
public Int32 IntField;
public String StringField;
public Decimal DecimalField;
public Guid GuidField;
private string m_UniqueKey;
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
public string UniqueKey
{
get
{
if (m_UniqueKey == null)
{
m_UniqueKey = IntField.ToString()
+ "|" + (StringField ?? string.Empty)
+ "|" + DecimalField.ToString("F6", CultureInfo.InvariantCulture)
+ "|" + GuidField.ToString("X");
}
return m_UniqueKey;
}
}
}
The code sample above computes m_UniqueKey only once (if it is null) and uses an arbitrary character as a separator between field values. It also try to format the decimal value to an arbtrary chosen precision.
In the case you need an hash/checksum value, you can try to implement GetHashCode() and rely on it, but also in this case you should include all important fields, or part of them.
Regards,
Daniele.