0

I know it's not very smart to use HashCode as a unique identifier,

but let's say I have two variables on the same HashCode is the only way I can get them,

how can I tell the difference?

Hodaya Shalom
  • 4,327
  • 12
  • 57
  • 111

2 Answers2

1

It's pretty much covered in the documentation. Assuming you have only the hash code:

The default implementation of the GetHashCode method does not guarantee unique return values for different objects. Furthermore, the .NET Framework does not guarantee the default implementation of the GetHashCode method, and the value it returns will be the same between different versions of the .NET Framework. Consequently, the default implementation of this method must not be used as a unique object identifier for hashing purposes.

From http://msdn.microsoft.com/en-us/library/system.object.gethashcode.aspx

Evan Trimboli
  • 29,900
  • 6
  • 45
  • 66
  • I read it, but let's say such a thing had happened and I can not change it, there's no way to know the differences between the two variables? – Hodaya Shalom Feb 03 '13 at 06:56
  • 1
    Assuming you have old the hash code, no. You could check if the object references are equal, but your question seems to imply that all you know is the hash code. – Evan Trimboli Feb 03 '13 at 07:07
1

You should be able to determine if the references are equal.

Try using the ReferenceEquals function to check if you have a reference to the same object.

Mash
  • 1,496
  • 13
  • 17
  • But `Equals` is used to test if the two objects are "like". This could be true even if there are two objects. For example with `string x = "abc"; string y = "a"; y += "bc";`, after the code has run, `x` and `y` have the same hash code. `Equals` returns `true` since `x` and `y` have the same string value. But `ReferenceEquals` returns `false` since `x` and `y` refer two objects, not the same one (unimportant that the two objects are actually identical). – Jeppe Stig Nielsen Feb 03 '13 at 08:24
  • @JeppeStigNielsen you are correct. However, I believe the asker was trying to determine if he has a reference to the same object, not if the values of the variables were equal. – Mash Feb 04 '13 at 00:34
  • 1
    If she (@HodayaShalom) is really only interested in object equality, instead of using `x.GetHashCode()` she could say `RuntimeHelpers.GetHashCode(x)`. That will (with a really high probability) give a unique code even if `x` and `y` are two object instances that are "equal" wrt. `Equals`. The overridden instance method `GetHashCode()` (no parameters) on the other hand must give a common hash for `x` and `y`. – Jeppe Stig Nielsen Feb 04 '13 at 10:20
  • @JeppeStigNielsen that is good to know. I didn't realize there was an overloaded method for getting the `HashCode` which could be used to determine object identity. That appears to be a suitable alternative. – Mash Feb 04 '13 at 15:38
  • If you have just two objects `x` and `y`, there's no need to check hash codes first, simply check `ReferenceEquals` directly. But if you're trying to build up a dictionary or hash set or similar based on `ReferenceEquals`, use the hash codes from `RuntimeHelpers.GetHashCode(object)` because that's the hash codes that "correspond" to `ReferenceEquals`. – Jeppe Stig Nielsen Feb 04 '13 at 16:05