Click here to Skip to main content
15,881,281 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
Hi,

while debugging C# very often I do miss an unique identifier for objects as I have known it from Java (Eclipse):
There each object has a unique ID during debugging and you can easily see if the object remains the same. This is very important if you want to make sure that two references are really the same and not only pointing to a clone of the object you are investigating.

Is there anything similar in C#?

Thank you very much
Andreas
Posted
Comments
Sergey Alexandrovich Kryukov 21-Jan-11 15:43pm    
Pretty good question. Please look at my answer. Sorry if you already knew all that. Please ask further questions if you have to.
Best,
--SA

Here is the alternative answer: using new Visual Studio debugging technique.

By some reason, very few developers knew about it. Here is what to do:

1) Start debugging (important, it won't work before);
2) Bring Main VS Menu -> Debug -> Windows -> Watch - Watch 1 (alternatively -- Immediate);
3) Proceed some debugging steps until your variables in question initialized, add them to Watch (not needed with Immediate);
4) On one of the initialized variables in question select the watch item, bring up the context menu and select "Make Object ID" (you may want to do "Delete Object ID" later.
5) Watch: the object in question will show unique ID you want. Also, referentially equal object will show the same ID, all other objects will not show anything.
6) Repeat (4-5) with other variables; that should spot all the stupid bugs and most clever ones;
7) Profit!

I experimented under debugger and pretty much sure that the unique IDs show in this way are exactly the same you would get using System.Runtime.Serialization.ObjectIDGenerator (please see my other answer).

When I said "adjust your debugging method", I meant exactly this. New way, not that you used to with Java, but better (if you think about it).
 
Share this answer
 
v6
Comments
Nish Nishant 21-Jan-11 18:12pm    
Voted 5, proposed as answer.
Sergey Alexandrovich Kryukov 21-Jan-11 18:18pm    
I agree this should be a primary method.
Thank you.
--SA
AndiHoffi 21-Jan-11 18:50pm    
That's 100% what I was looking for! I just tried it and it was working perfectly fine. Still need to try this with different threads, but I am very optimistic that this will work there as well.

Thank you very much :)
Sergey Alexandrovich Kryukov 21-Jan-11 18:55pm    
My pleasure! Best wishes to reach item (7) as much as possible.
Call again.
--SA
Sergey Alexandrovich Kryukov 21-Jan-11 19:03pm    
Threads will make no difference at all. All objects live in a single address space of the Application Domain. Hence, this is a technique of unique IDs per Application Domain.
It's not something like missing feature.
There are good reasons for not adding such feature.

However, I understand your frustration about debugging. I think you need to adjust your debugging method (please see another Answer I offer -- alternative method).

Now, please don't blame me if you already knew what I'm going to advise. Anyway:

1) You need to make a fundamental use of object.ReferenceEquals.
2) You can create a unique ID for a reference object using System.Runtime.Serialization.ObjectIDGenerator. (Be careful with primitive or enumeration objects -- they will be boxed and then you would get a unique ID of boxed object, which does not make a primitive-type object unique, but no one would need this anyway.)
3) You can develop your own unique ID for your objects (better under "#if debug"); you could use GUID, but better be static global integer value made thread-safe using static lock object, increment on each constructor of each of your classes in question.

Please, don't hesitate to ask if any of the above items is not clear or you need further help.
 
Share this answer
 
v4
Comments
Estys 21-Jan-11 17:02pm    
In Eclipse debugging every variable shows a simple unique identifier, like (123), that gives an easy cue to see which object (instance) reference is present. The watch window of VS does not give such a clue. I think that's his question.
Sergey Alexandrovich Kryukov 21-Jan-11 18:09pm    
Thank you; please see my alternative method -- another Answer.
--SA
Sergey Alexandrovich Kryukov 21-Jan-11 20:16pm    
Also, look at ObjectIDGenerator -- update to the current Answer
Member 14735577 19-Feb-20 9:16am    
Please, can you give an application example ?
 
Share this answer
 
v2
Comments
AndiHoffi 21-Jan-11 11:08am    
I was afraid, that this discussion will be referenced :)
And I had (still have) the hope that something changed since then.
Sergey Alexandrovich Kryukov 21-Jan-11 18:24pm    
It has already changed well enough, see my answer on debugging method with VS. The Java was is inferior: why carrying those unwanted IDs all the time.
--SA
Sergey Alexandrovich Kryukov 21-Jan-11 15:42pm    
I don't this this answer is relevant. Would you like to look at mine.
(I did not vote.)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900