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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T02:55:47+00:00 2026-06-08T02:55:47+00:00

Possible Duplicate: Why might a System.String object not cache its hash code? I always

  • 0

Possible Duplicate:
Why might a System.String object not cache its hash code?

I always thought that, given that .Net strings are immutable, String.GetHashCode() didn’t have to calculate the hash every time it’s called — if the characters won’t change, the hash is constant for a given instance of System.String, I naively thought; String.GetHashCode() could have O(1) complexity.

Reverse engineering it shattered this assumption.

Of course, hash codes aren’t meant to be constant and so on, but what could prevent a String implementation from having hash codes already calculated from, say, construction time?

  • 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-08T02:55:48+00:00Added an answer on June 8, 2026 at 2:55 am

    Good question!

    I asked the same thing a while back.

    Basically, it’s a speed/memory trade-off. The benefit of caching string hash codes is arguably outweighed by the overhead of every single string object requiring another 32 bits of memory to be allocated. This makes sense when you think about the large number of strings that may exist in a program versus the number whose hash codes you care about (presumably because you’re using them as keys).

    The latter number may be large in some programs, but it may also be quite small. It could even be zero in a lot of cases.

    If performance were an extreme concern for you in certain scenarios, you might consider writing your own wrapper that does cache its hash code:

    public class StringKey
    {
        string value;
        int hashCode;
    
        public StringKey(string value)
        {
            this.value = value;
            this.hashCode = value.GetHashCode();
        }
    
        public override int GetHashCode()
        {
            return this.hashCode;
        }
    
        public override string ToString()
        {
            return this.value;
        }
    
        // Plus all the other stuff you'd want to include here,
        // e.g., Equals, CompareTo, etc.
    }
    

    To get any benefit out of this, of course, you’d still need to be very careful to reuse these StringKey objects basically throughout your program everywhere. In the vast majority of cases, this would not be worth the effort; I’ve included the idea only as something to consider if you happen to be an exceptional case.

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

Sidebar

Related Questions

Possible Duplicate: Howto build a SQL statement with using IDs that might not be
Possible Duplicate: Notepad++ code snippets manager I realize that Notepad++ is not an IDE
Possible Duplicate: class System.Random .. why not static? Following on from Generated random numbers
Possible Duplicate: C++ variable types limits I have a defined type that may not
Possible Duplicate: How might I extract the property values of a JavaScript object into
Possible Duplicate: When do you use code blocks? Ok, this might be a stupid
Possible Duplicate: How is std::iostream buffered? This might sound ridiculous, but how can I
Possible Duplicate: && operator in Javascript In the sample code of the ExtJS web
Possible Duplicate: System.Random keeps on returning the same value I'm refactoring and expanding a
Possible Duplicate: What are the internal and external databases that can be used with

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.