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

  • Home
  • SEARCH
  • 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 3996528
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:17:25+00:00 2026-05-20T07:17:25+00:00

To quote from Guidelines and rules for GetHashCode by Eric Lippert: Rule: Consumers of

  • 0

To quote from Guidelines and rules for GetHashCode by Eric Lippert:

Rule: Consumers of GetHashCode cannot rely upon it being stable over time or across appdomains

Suppose you have a Customer object
that has a bunch of fields like Name,
Address, and so on. If you make two
such objects with exactly the same
data in two different processes, they
do not have to return the same hash
code. If you make such an object on
Tuesday in one process, shut it down,
and run the program again on
Wednesday, the hash codes can be
different.

This has bitten people in the past.
The documentation for
System.String.GetHashCode notes
specifically that two identical
strings can have different hash codes
in different versions of the CLR, and
in fact they do. Don’t store string hashes in databases and expect them to be the same forever, because they won’t be.

So what is the correct way to create a HashCode of a string that I can store in a database?

(Please tell me I am not the first person to have left this bug in software I have written!)

  • 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-20T07:17:26+00:00Added an answer on May 20, 2026 at 7:17 am

    It depends what properties you want that hash to have. For example, you could just write something like this:

    public int HashString(string text)
    {
        // TODO: Determine nullity policy.
    
        unchecked
        {
            int hash = 23;
            foreach (char c in text)
            {
                hash = hash * 31 + c;
            }
            return hash;
        }
    }
    

    So long as you document that that is how the hash is computed, that’s valid. It’s in no way cryptographically secure or anything like that, but you can persist it with no problems. Two strings which are absolutely equal in the ordinal sense (i.e. with no cultural equality etc applied, exactly character-by-character the same) will produce the same hash with this code.

    The problems come when you rely on undocumented hashing – i.e. something which obeys GetHashCode() but is in no way guaranteed to remain the same from version to version… like string.GetHashCode().

    Writing and documenting your own hash like this is a bit like saying, “This sensitive information is hashed with MD5 (or whatever)”. So long as it’s a well-defined hash, that’s fine.

    EDIT: Other answers have suggested using cryptographic hashes such as SHA-1 or MD5. I would say that until we know there’s a requirement for cryptographic security rather than just stability, there’s no point in going through the rigmarole of converting the string to a byte array and hashing that. Of course if the hash is meant to be used for anything security-related, an industry-standard hash is exactly what you should be reaching for. But that wasn’t mentioned anywhere in the question.

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

Sidebar

Related Questions

For example, mysql quote table name using SELECT * FROM `table_name`; notice the `
I remember some rules from a time ago (pre-32bit Intel processors), when was quite
From Flickr's community guidelines: Do link back to Flickr when you post your photos
Quote from: http://msdn.microsoft.com/en-us/library/aa645739(VS.71).aspx "Invoking an event can only be done from within the class
A quote from persistence.xml : <persistence-unit name=test transaction-type=RESOURCE_LOCAL> <properties> <property name=hibernate.archive.autodetection value=class /> <property
So here is the situation-- a quote from my boss: [...] we need to
From the haskell report: The quot, rem, div, and mod class methods satisfy these
I have a quite big XML output from an application. I need to process
I know from reading Microsoft documentation that the "primary" use of the IDisposable interface
I have to copy quite a lot of files from one folder to another.

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.