Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print object ID?

Tags:

c#

`I need to know if two references from completely different parts of the program refers to the same object. I can not compare references programaticaly because they are from the different context (one reference is not visible from another and vice versa).

Then I want to print unique identifier for each object using Console.WriteLine(). But ToString() method doesn't return "unique" identifier, it just returns "classname".

Is it possible to print unique identifier in C# (like in Java)?

like image 287
Oleg Vazhnev Avatar asked Apr 18 '11 14:04

Oleg Vazhnev


People also ask

How do you print an object ID in Python?

Python id() FunctionThe id() function returns a unique id for the specified object. All objects in Python has its own unique id. The id is assigned to the object when it is created.

What is object ID in Java?

ObjectId(java.util.Date date, int counter) Constructs a new instances using the given date and counter. ObjectId(java.util.Date date, int machineIdentifier, short processIdentifier, int counter) Constructs a new instances using the given date, machine identifier, process identifier, and counter.

Does the ID of an object change Python?

Python id() function returns the “identity” of the object. The identity of an object is an integer, which is guaranteed to be unique and constant for this object during its lifetime. Two objects with non-overlapping lifetimes may have the same id() value.


2 Answers

I presume you're calling ToString on your object reference, but not entirely clear on this or your explained situatyion, TBH, so just bear with me.

Does the type expose an ID property? If so, try this:

var idAsString = yourObjectInstance.ID.ToString();

Or, print directly:

Console.WriteLine(yourObjectInstance.ID);

EDIT:

I see Jon seen right through this problem, and makes my answer look rather naive - regardless, I'm leaving it in if for nothing else but to emphasise the lack of clarity of the question. And also, maybe, provide an avenue to go down based on Jon's statement that 'This [GetHashCode] is still not a unique identifier', should you decide to expose your own uniqueness by way of an identifier.

like image 151
Grant Thomas Avatar answered Sep 28 '22 17:09

Grant Thomas


The closest you can easily get (which won't be affected by the GC moving objects around etc) is probably RuntimeHelpers.GetHashCode(Object). This gives the hash code which would be returned by calling Object.GetHashCode() non-virtually on the object. This is still not a unique identifier though. It's probably good enough for diagnostic purposes, but you shouldn't rely on it for production comparisons.

EDIT: If this is just for diagnostics, you could add a sort of "canonicalizing ID generator" which was just a List<object>... when you ask for an object's "ID" you'd check whether it already existed in the list (by comparing references) and then add it to the end if it didn't. The ID would be the index into the list. Of course, doing this without introducing a memory leak would involve weak references etc, but as a simple hack this might work for you.

like image 33
Jon Skeet Avatar answered Sep 28 '22 17:09

Jon Skeet