13

As far as i know, there is no direct equivalent in C#. My current idea is to use a Dictionary with a custom IEqualityComparer, that checks for reference equality.

However, this seems to lose the advantage gained by hashing. Is there a way to get an individual hashcode out of every different object? Or is this impossible and I should use some other approach?

mafu
  • 31,798
  • 42
  • 154
  • 247

1 Answers1

19

You can use RuntimeHelpers.GetHashCode(object) which calls object.GetHashCode() non-virtually - this is the equivalent of System.identityHashcode in Java. I think that's what you're after. So your IEqualityComparer would just use that for hashing and object.ReferenceEquals for equality.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • http://stackoverflow.com/questions/8946790/how-to-use-an-objects-identity-as-key-for-dictionaryk-v – Yoseph May 21 '17 at 05:07