0

I need to build a stable (= does not vary with time) hashcode of an object. Specifically, I do not know the exact type of the object. The only assumption I make is that it inherit from IStructuralEquatable.

So I need a method:

static string GetStableHashcode <T> (T object) where T: IStructuralEquatable

The .Net framework provide this type of function? Otherwise, which algorithm to use?

nlips
  • 1,258
  • 9
  • 25
  • Your question confuses me somewhat. Normally a hash codes do not vary over time and they are integers, not strings. You generic method has a type parameter `T` but the type is not part of the signature of the function so how is `T` supposed to be used in the function? Anyway, you can use .NET tuples or anonymous types to create hash codes by combining values but I am not sure this answers your question. – Martin Liversage Apr 24 '14 at 08:53
  • Yes the function can return an integer (so I have just to serialize it if I need a string). The `Object.GetHashCode` (on Tuple or any other .Net class) aren't stable. 2 execution instance (so 2 AppDomain) will produce 2 different hashcode for a same object!?! – nlips Apr 24 '14 at 09:00

2 Answers2

0

Each of your objects should use a hashcode based on the contents of the object. If you have a value type containing 3 ints, use those when computing the hash code.

Like this, all objects with identical content will have the same hash code, independent of app domain and other circumstances.

An example for a good hash code can be found in this answer.

Community
  • 1
  • 1
Wilbert
  • 7,251
  • 6
  • 51
  • 91
  • This is not a generic solution. Objects are not _my objects_. Those are just objects that implements `IStructuralEquatable` – nlips Apr 24 '14 at 09:31
  • But the contract for structural equatable requires that all objects that have the same content values are equal. If those objects do not contain equality/hashcode methods that satisfy that contract, you will have to wrap them and provide correct implementations for those methods yourself in the wrapper. – Wilbert Apr 24 '14 at 10:52
0

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.

Martin Liversage
  • 104,481
  • 22
  • 209
  • 256