14

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?

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.