How can I generate a hash code for an object based on its identity.
What I mean is that:
- if
object.ReferenceEquals(a, b) == true
, thena
andb
will get the same hash code. - if
object.ReferenceEquals(a, b) == false
, thena
andb
should have a decent chance to get different hash codes even if they are memberwise equal.
What I have is:
class SomeClassThatMakesSenseToCompareByReferenceAndByValue {
override Equals(object o) {
return MemberwiseEquals(o);
}
override GetHashCode() {
return MemberwiseGetHashCode();
}
}
class SomeClassThatNeedsReferenceComparison {
SomeClassThatMakesSenseToCompareByReferenceAndByValue obj;
override Equals(object o) {
return o is SomeClassThatNeedsReferenceComparison && object.ReferenceEquals(this.obj, (o as SomeClassThatNeedsReferenceComparison).obj);
}
override GetHashCode() {
return ?????
}
}