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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T22:38:55+00:00 2026-05-11T22:38:55+00:00

I have an object for which I want to generate a unique hash (override

  • 0

I have an object for which I want to generate a unique hash (override GetHashCode()) but I want to avoid overflows or something unpredictable.

The code should be the result of combining the hash codes of a small collection of strings.

The hash codes will be part of generating a cache key, so ideally they should be unique however the number of possible values that are being hashed is small so I THINK probability is in my favour here.

Would something like this be sufficient AND is there a better way of doing this?

int hash = 0;
foreach(string item in collection){
    hash += (item.GetHashCode() / collection.Count)
}
return hash;

EDIT: Thanks for answers so far.
@Jon Skeet: No, order is not important

I guess this is almost a another question but since I am using the result to generate a cache key (string) would it make sense to use a crytographic hash function like MD5 or just use the string representation of this int?

  • 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-11T22:38:55+00:00Added an answer on May 11, 2026 at 10:38 pm

    The fundamentals pointed out by Marc and Jon are not bad but they are far from optimal in terms of their evenness of distribution of the results. Sadly the ‘multiply by primes’ approach copied by so many people from Knuth is not the best choice in many cases better distribution can be achieved by cheaper to calculate functions (though this is very slight on modern hardware). In fact throwing primes into many aspects of hashing is no panacea.

    If this data is used for significantly sized hash tables I recommend reading of Bret Mulvey’s excellent study and explanation of various modern (and not so modern) hashing techniques handily done with c#.

    Note that the behaviour with strings of various hash functions is heavily biased towards wehther the strings are short (roughly speaking how many characters are hashed before the bits begin to over flow) or long.

    One of the simplest and easiest to implement is also one of the best, the Jenkins One at a time hash.

    private static unsafe void Hash(byte* d, int len, ref uint h)
    {
        for (int i = 0; i < len; i++)
        {
            h += d[i];
            h += (h << 10);
            h ^= (h >> 6);
        }
    }
    
    public unsafe static void Hash(ref uint h, string s)
    {
        fixed (char* c = s)            
        {
            byte* b = (byte*)(void*)c;
            Hash(b, s.Length * 2, ref h);
        }
    }
    
    public unsafe static int Avalanche(uint h)
    {
        h += (h<< 3);   
        h ^= (h>> 11);  
        h += (h<< 15);  
        return *((int*)(void*)&h);
    }
    

    you can then use this like so:

    uint h = 0;
    foreach(string item in collection) 
    {
        Hash(ref h, item);
    }
    return Avalanche(h);
    

    you can merge multiple different types like so:

    public unsafe static void Hash(ref uint h, int data)
    { 
        byte* d = (byte*)(void*)&data;
        AddToHash(d, sizeof(int), ref h);
    }
    
    public unsafe static void Hash(ref uint h, long data)
    { 
        byte* d= (byte*)(void*)&data;
        Hash(d, sizeof(long), ref h);
    }
    

    If you only have access to the field as an object with no knowledge of the internals you can simply call GetHashCode() on each one and combine that value like so:

    uint h = 0;
    foreach(var item in collection) 
    {
        Hash(ref h, item.GetHashCode());
    }
    return Avalanche(h);
    

    Sadly you can’t do sizeof(T) so you must do each struct individually.

    If you wish to use reflection you can construct on a per type basis a function which does structural identity and hashing on all fields.

    If you wish to avoid unsafe code then you can use bit masking techniques to pull out individual bits from ints (and chars if dealing with strings) with not too much extra hassle.

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

Sidebar

Related Questions

I have a 3d object which I want to keep in the center of
I have an object which I first want to rotate (about its own center)
I have this object which is an instance of a superclass. I want to
I have a productVariant object which has child product object. I want to show
I have a page in which I want to maintain the value of object
i have an MJPEG stream over RTSP/UDP from which i want to generate JPEGs
I have a list of objects which I want to turn into a set.
I have a list of person objects which I want to send in response
I have 2 objects, both of which I want to convert to dictionarys. I
I have a List which contains a list of objects and I want to

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.