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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:49:23+00:00 2026-06-12T10:49:23+00:00

My objects in a complex structure have a property Dictionary<Object, Object> Annotations , where

  • 0

My objects in a complex structure have a property Dictionary<Object, Object> Annotations, where I can store meta and custom data used during some processing phases. I can now create a static readonly object UniqueName = new object() and use that as the key in the dictionary. I use a static readonly object because I know for sure that it is unique. No one else can ever create the exact same object instance. If instead I had used a string as a key, someone else could accidentally use the same string as the key and this could cause problems.

However, I understood from several sources (including here) that the default GetHashCode implementation is based on the location of the object in memory. This may change when the object is moved by the garbage collector, and this would cause the hash it returns to change. When that happens, the dictionary will be unable to find the value associated with the key object.

How can I ensure that an object that has no fields never changes its hash code during its life time?

The naive solution is to create a Key object whose GetHashCode always returns 42. However, this would severely impact the performance of the dictionary and is therefore not a solution.

  • 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-12T10:49:25+00:00Added an answer on June 12, 2026 at 10:49 am

    The default implementation of GetHashCode returns an index, rather than a memory address. This index is unique for the lifetime of the object, so even if your object is moved around in memory it will always return the same value when you call GetHashCode

    However, once the object is garbage collected it valid for a new object you create afterwards to return the same value as a previous object did prior to garbage collection.

    In your example the UniqueName variable will always return the same value when you call GetHashCode, and no instance of Object that you create will ever return the same hash code for the lifetime of your program.

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

Sidebar

Related Questions

I have to transfer objects between activities. Objects are with complex structure. I'm not
Can I automatically validate complex child objects when validating a parent object and include
I have a web service that returns a complex data structure representing the results
I'm trying to create a controller for a custom complex object but have some
I have a complex Python data structure (if it matters, it's a large music21
I have two objects: oldObj and newObj . The data in oldObj was used
I have a complex object(tree structure) which I am flattening it out into a
There are several different ways I can initialize complex objects (with injected dependencies and
I have a complex network of objects being spawned from a sqlite database using
I have a list of objects, that are pre-sorted based on some complex criteria

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.