Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6675045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:48:19+00:00 2026-05-26T03:48:19+00:00

In the Notes to Implementers section in the documentation for the GetHashCode method of

  • 0

In the “Notes to Implementers” section in the documentation for the GetHashCode method of the IEqualityComparer<T> interface, it states:

Implementations are required to ensure that if the Equals method
returns true for two objects x and y, then the value returned by the
GetHashCode method for x must equal the value returned for y.

It’s well known why you want two instances of T to return the same hash code in the event that two items are equal; them being different would mean they are not equal, while them being the same means that they are potentially equal.

I interpret the quote to be undefined for the return value when two instances are not equal (even though their values might suggest so).

Take the following for example. I have a sequence of int? which I want to use for statistical classification where each non-null int? represents an attribute on a class (think enum values). In the case where these values are null, you don’t want the values to be considered equal, as they would bias the training set towards missing values. If anything, you would want null values in this case when compared to other null values to return false.

The thing is, in the GetHashCode method, when given a null, I’d probably want to return 0 (or some other number, maybe Int32.MinValue). Now, I know that when anything is keyed using this IEqualityComparer<T> implementation, the performance for checking for existence of a key in the dictionary is not going to be optimal for those cases.

That said, is it valid to return a value known to conflict for other values when GetHashCode is called, when the call to Equals returns false? I’m leaning towards yes, as the quote above is undefined in this matter.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T03:48:19+00:00Added an answer on May 26, 2026 at 3:48 am

    It is absolutely necessary for almost all types for there to be two values v1 and v2 such that

    v1.Equals(v2) == false
    v1.GetHashCode() == v2.GetHashCode()
    

    … or the equivalent with an IEqualityComparer<T>. The only cases where that isn’t the case are where there are at most 232 distinct (non-equal) values. As soon as there are more values than that, the pigeon-hole principle forces hash codes to be reused – there just aren’t enough hash codes to go round!

    Eric Lippert had a great blog post on hash codes which is well worth a read. Basically I think you’ve got the right ideas, but it’s worth reinforcing them.

    The issue of nulls is an interesting one, by the way. IEqualityComparer<T> allows GetHashCode to throw an exception, but I believe the built-in Comparer<T> implementations never do. It sounds like you do have one problem though – that Equals should be reflexive – so a null value should be equal to itself. You may need to think about that one carefully… can you represent “different” null values?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

http://leepoint.net/notes-java/data/expressions/22compareobjects.html It turns out that defining equals() isn't trivial; in fact it's moderately hard
Fixed: See notes at bottom I am implementing a generic class that supports two
None of the guides/notes/articles that discuss IDisposable pattern suggest that one should set the
I have a stored procedure that, depending on the input, returns multiple rows of
Coles Notes version: index.php?map_id=foo is loaded into iframe on www.not-my-domain.com. index sets SESSION['map_id'] =
C++ Notes: Array Initialization has a nice list over initialization of arrays. I have
I am using Lotus Notes as my email client, and how to change the
The following SQL: SELECT notes + 'SomeText' FROM NotesTable a Give the error: The
What are release notes for and who reads them? Should/could they be automated by
Earlier versions of the Notes client would automatically turn a sent URL into a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.