5

I have a class A that implements IEquatable<>, using its fields (say, A.b and A.c) for implementing/overriding Equals() and overriding GetHashCode(), and everything works fine, 99% of the time. Class A is part of a hierarchy (class B, C) that all inherit from interface D; they can all be stored together in a dictionary Dictionary, thus it's convenient when they all carry their own default Equals()/GetHashCode().

However, while constructing A I sometime need to do some work to get the values for A.b and A.c; while that's happening, I want to store a reference to the instance that's being built. In that case, I don't want to use the default Equals()/GetHashCode() overrides provided by A. Thus, I was thinking of implementing a ReferenceEqualityComparer, that's meant to force the use of Object's Equals()/GetHashCode():

    private class ReferenceEqualityComparer<T> : IEqualityComparer<T>
    {
        #region IEqualityComparer<T> Members
        public bool Equals(T x, T y)
        {
            return System.Object.ReferenceEquals(x, y);
        }

        public int GetHashCode(T obj)
        {
            // what goes here? I want to do something like System.Object.GetHashCode(obj);
        }
        #endregion
    }

The question is, since A overrides Object.GetHashCode(), how can I (outside of A) call Object.GetHashCode() for an instance of A?

One way of course would be for A to not implement IEquatable<> and always supply an IEqualityComparer<> to any dictionary that I create, but I'm hoping for a different answer.

Thanks

1
  • 2
    You are digging yourself a pretty deep hole. Get out of it with IEqualityComparer<>. Commented Jan 23, 2010 at 17:13

2 Answers 2

7

The natural match for object.ReferenceEquals is RuntimeHelpers.GetHashCode.

See the answer to this question for full details and an implementation of ObjectReferenceEqualityComparer<T>: Built-in IEqualityComparer<T> that uses ReferenceEquals

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

Comments

-1

Call the CLR's base implementation via interop: Default implementation for Object.GetHashCode()

2 Comments

The link is interesting for academic purposes, but you should really use RuntimeHelpers.GetHashCode() instead.
True, didn't know about that one. It does just call exactly the same underlying interop method, so there's no functional difference (but yeah: you should use the published API in case it changes etc...)

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.