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 7797663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T23:45:23+00:00 2026-06-01T23:45:23+00:00

Sorry to combine two questions into one, they are related. HashCode s for HashSet

  • 0

Sorry to combine two questions into one, they are related.

HashCodes for HashSets and the such. As I understand it, they must be unique, not change, and represent any configuration of an object as a single number.

My first question is that for my object, containing the two Int16s a and b, is it safe for my GetHashCode to return something like a * n + b where n is a large number, I think perhaps Math.Pow(2, 16)?

Also GetHashCode appears to inflexibly return specifically the type Int32.

32bits can just about store, for example, two Int16s, a single unicode character or 16 N, S, E, W compass directions, it’s not much, even something like a small few node graph would probably be too much for it. Does this represent a limit of C# Hash collections?

  • 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-06-01T23:45:25+00:00Added an answer on June 1, 2026 at 11:45 pm

    As I understand it, they must be unique

    Nope. They can’t possibly be unique for most types, which can have more than 232 possible values. Ideally, if two objects have the same hash code then they’re unlikely to be equal – but you should never assume that they are equal. The important point is that if they have different hash codes, they should definitely be unequal.

    My first question is that for my object, containing the two Int16s a and b, is it safe for my GetHashCode to return something like a * n + b where n is a large number, I think perhaps Math.Pow(2, 16).

    If it only contains two Int16 values, it would be simplest to use:

    return (a << 16) | (ushort) b;
    

    Then the value will be unique. Hoorah!

    Also GetHashCode appears to inflexibly return specifically the type Int32.

    Yes. Types such as Dictionary and HashSet need to be able to use the fixed size so they can work with it to put values into buckets.

    32bits can just about store, for example, two Int16s, a single unicode character or 16 N, S, E, W compass directions, it’s not much, even something like a small few node graph would probably be too much for it. Does this represent a limit of C# Hash collections?

    If it were a limitation, it would be a .NET limitation rather than a C# limitation – but no, it’s just a misunderstanding of what hash codes are meant to represent.

    Eric Lippert has an excellent (obviously) blog post about GetHashCode which you should read for more information.

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

Sidebar

Related Questions

sorry to combine two questions into one but I'm having trouble finding and answer
Sorry if this is a noob question but is there a way to combine
So, I've been asking a lot of Xpath questions recently. Sorry, but I've only
I have two IList<Traffic> I need to combine. Traffic is a simple class: class
I am a new one in Android so please sorry for stupidity. Well my
Sorry for the basic question - I'm a .NET developer and don't have much
Sorry for this not being a real question, but Sometime back i remember seeing
Sorry, I'm new to SVN and I looked around a little for this. How
Sorry the title isn't more help. I have a database of media-file URLs that
Sorry for the second newbie question, I'm a developer not a sysadmin so this

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.