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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T14:43:05+00:00 2026-05-11T14:43:05+00:00

Is there a way of getting a unique identifier of an instance? GetHashCode() is

  • 0

Is there a way of getting a unique identifier of an instance?

GetHashCode() is the same for the two references pointing to the same instance. However, two different instances can (quite easily) get the same hash code:

Hashtable hashCodesSeen = new Hashtable(); LinkedList<object> l = new LinkedList<object>(); int n = 0; while (true) {     object o = new object();     // Remember objects so that they don't get collected.     // This does not make any difference though :(     l.AddFirst(o);     int hashCode = o.GetHashCode();     n++;     if (hashCodesSeen.ContainsKey(hashCode))     {         // Same hashCode seen twice for DIFFERENT objects (n is as low as 5322).         Console.WriteLine('Hashcode seen twice: ' + n + ' (' + hashCode + ')');         break;     }     hashCodesSeen.Add(hashCode, null); } 

I’m writing a debugging addin, and I need to get some kind of ID for a reference which is unique during the run of the program.

I already managed to get internal ADDRESS of the instance, which is unique until the garbage collector (GC) compacts the heap (= moves the objects = changes the addresses).

Stack Overflow question Default implementation for Object.GetHashCode() might be related.

The objects are not under my control as I am accessing objects in a program being debugged using the debugger API. If I was in control of the objects, adding my own unique identifiers would be trivial.

I wanted the unique ID for building a hashtable ID -> object, to be able to lookup already seen objects. For now I solved it like this:

Build a hashtable: 'hashCode' -> (list of objects with hash code == 'hashCode') Find if object seen(o) {     candidates = hashtable[o.GetHashCode()] // Objects with the same hashCode.     If no candidates, the object is new     If some candidates, compare their addresses to o.Address         If no address is equal (the hash code was just a coincidence) -> o is new         If some address equal, o already seen } 
  • 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. 2026-05-11T14:43:06+00:00Added an answer on May 11, 2026 at 2:43 pm

    The reference is the unique identifier for the object. I don’t know of any way of converting this into anything like a string etc. The value of the reference will change during compaction (as you’ve seen), but every previous value A will be changed to value B, so as far as safe code is concerned it’s still a unique ID.

    If the objects involved are under your control, you could create a mapping using weak references (to avoid preventing garbage collection) from a reference to an ID of your choosing (GUID, integer, whatever). That would add a certain amount of overhead and complexity, however.

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

Sidebar

Related Questions

Is there a way of getting around circular unit references in Delphi? Maybe a
Is there a portable way of getting thread and/or process identifier (string, int, ...)
Is there a way of getting all required assemblies (excluding the .net framework) for
Is there a way of getting the process id of my C++ application? I
Is there a way of getting the websites absolute URL ( http://www.domain.com/ ) using
Is there a way of getting the position (index) of an item in a
Is there a way in getting the row number for items displayed in Paginator?
In C# Is there a way of getting a finer granularity with SQL exceptions?
Using the Facebook API, is there a way of getting a friend's phone/cell number?
Is there a simple way of getting a HTML textarea and an input type=text

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.