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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:18:21+00:00 2026-05-23T03:18:21+00:00

I had the following code to generate a hash of an object: public int

  • 0

I had the following code to generate a hash of an object:

public int GetHashCode(MyType obj)
{
   return (obj.Prop1.GetHashCode() + obj.Prop2.GetHashCode() + obj.Prop3.GetHashCode()).GetHashCode();
}

I.e. I add all the properties’ hash codes and then take the hash of this.

In review, a coworker suggested that this will collide too frequently. I’m not sure that this is true because:

  1. Given that hash codes are chosen with equal frequency among positive and negative numbers and they wrap around, I don’t think there’s any additional information we gain about the likelihood of these numbers’ sum as opposed to the numbers themselves
  2. To the extent that their sum is non-random, hash codes are designed to make numbers that are “close together” become “far apart”, so feeding a non-uniformly-distributed value into the function shouldn’t be an issue

Who is correct?

It is in C#, in case the answer is language-specific.

  • 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-23T03:18:21+00:00Added an answer on May 23, 2026 at 3:18 am

    Yes.

    Just suppose Prop1, Prop2 etc are of type int. Usually only the lower range of integers is used. Your sum approach will collide more often than necessary.

    The HasCode of 7 is 7, which makes perfect sense when hashing int by it self. But with your code the tuples <7, 3>, <3, 7> and <8, 2> will all have the same Hash. The same with simple XOR instead of Addition.

    The common approach is to add some (prime) numbers and shifting:

    public int GetHashCode(MyType obj)
    {
      int hash = 0;
      unchecked
      {         
         hash += 19 * obj.Prop1.GetHashCode();
         hash += 31 * obj.Prop2.GetHashCode();
         hash += 37 * obj.Prop3.GetHashCode();
      }
      return hash;
    }
    

    The numbers 19, 31, 37 are not too critical. And if you prefer you can use OR or XOR instead of + .

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

Sidebar

Related Questions

Say I had the following code: % Cellmode_subfunction_test.m %% Cell 1 foo(1); %% Cell
I had the following line snippet of code that searches for a propery of
I've used following code to generate database creation script Configuration configuration = new Configuration();
I had the following code in a test.I am confused about what (i,j) evaluates,while
I have the following business object: public class MyObject { // Some public properties
Please can you tell me where I am wrong. I had a following code:
If I had the following select, and did not know the value to use
A long time ago I had the following directory structure in my SVN repository
What would be the best hashing algorithm if we had the following priorities (in
Conceptually, I would like to accomplish the following but have had trouble understand how

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.