There is no need to use generics in your function so your question is how to implement the method
static string GetStableHashcode(IStructuralEquatable obj) { ... }
And the only sensible solution is to implement it like this:
static string GetStableHashcode(IStructuralEquatable obj) {
return obj.GetHashCode().ToString();
}
Your concern is that Object.GetHashCode()
does not provide values that are stable and the concern is very valid as can be seen in the first box headed by Caution in the documentation:
Do not serialize hash code values or store them in databases.
[...]
Do not send hash codes across application domains or processes. In some cases, hash codes may be computed on a per-process or per-application domain basis.
Actually, some hash codes created by Object.GetHashCode
are stable like Int32.GetHashCode
and String.GetHashCode
and the algorithm used by Tuple.GetHashCode
will also combine hash codes in a "stable manner". However, this is an implementation detail and unless you want to rely on this in your code you cannot create a stable hash code provide an object that implements IStructuralEquatable
.