13

`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)?

Oleg Vazhnev
  • 23,239
  • 54
  • 171
  • 305
  • 1
    Can you give a use case where this makes sense? Trying to compare objects without their references is meaningless. – Tejs Apr 18 '11 at 14:05

4 Answers4

24

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Is Blindy's answer not correct? Isn't this whole question moot? – Adam Rackis Apr 18 '11 at 14:17
  • @Adam: See my comment on Blindy's answer. Only guessing based on the question, but it makes a certain amount of sense. – Jon Skeet Apr 18 '11 at 14:21
  • thanks, that's work fine for debugging. In my cases values were different so now I know that objects are different. But I guess if values are the same it doesn't mean that objects are the same. – Oleg Vazhnev Apr 18 '11 at 14:27
3

one reference is not visible from another and vice versa

I don't buy that. If you couldn't even get the handles, how would you get their ID's?

In C# you can always get handles to objects, and you can always compare them. Even if you have to use reflection to do it.

Blindy
  • 65,249
  • 10
  • 91
  • 131
  • 3
    I suspect you're missing how the OP is trying to diagnose a problem... printing out an object reference of some description at one point in time, and then printing out another, then comparing them visually. At no point in that process does any one piece of code have access to both references simultaneously. That's my guess, anyway. – Jon Skeet Apr 18 '11 at 14:19
  • 1
    @Jon, he could always add them to a "global" list and inspect the list contents at some point in time with `object.Equals` if that's his intent. I'll admit however that I hadn't thought of any practical application of his problem <. – Blindy Apr 18 '11 at 14:26
1

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.

Grant Thomas
  • 44,454
  • 10
  • 85
  • 129
1

If you need to know if two references are pointing the same object, I'll just citate this.

By default, the operator == tests for reference equality. This is done by determining if two references indicate the same object. Therefore reference types do not need to implement operator == in order to gain this functionality.

So, == operator will do the trick without doing the Id workaround.

apacay
  • 1,702
  • 5
  • 19
  • 41
  • 3
    "I can not compare references programaticaly" – Jon Skeet Apr 18 '11 at 14:06
  • Then my answer is "No it's not possible" Unless you create a CustomId ei a GUID and compare those. Or do the GetHashCode() workaround you told there. – apacay Apr 18 '11 at 14:15