Make Object ID

Tue, August 26, 2008, 12:28 AM under dotNET
I only learnt about this featurette very recently (from a reader's comment) and immediately found a use for it – read on.

If you tried to debug the code from this exercise (still no correct answers to the quiz!), you probably found out that the variable corresponding to the Task that you want to examine is usually out of scope. You can look at the Threads window and try and map the Thread to the Task but there is another way: add once to the Watch window the expression Task.Current. When breaking in the debugger, switch between the threads in the Threads window to see the active/current Task for that Thread in the Watch window.

The only problem is that we have to tediously expand the variable each time to determine its Id, since the default display when the expression is evaluated is not useful ("{System.Threading.Tasks.Task}").

This is where the C#-only debugger feature comes in: Make Object ID, accessible from the ContextMenu in the Watch window. Once you break in the debugger, Make Object ID for the Task.Current, then switch Threads and do the same for the other. Now you can distinguish between them easily due to the appended {1#}, {2#}, and you can add a Watch directly to the objects in memory:

This is a neat little feature and even more useful for other scenarios where there is a need to distinguish between 2 or more identical-looking objects in the debugger.
Tuesday, 26 August 2008 18:59:00 (Pacific Daylight Time, UTC-07:00)
This is a great feature indeed, especially because it allows to visualize objects that are out of scope and it is a fully recognized object, so you can use it wherever you want in the Immediate window or Watch window also as part of far more complex expressions (like GC.GetGeneration(1#)).
bart
Friday, 13 February 2009 08:28:58 (Pacific Standard Time, UTC-08:00)
Exactly...
Friday, 11 September 2009 07:30:14 (Pacific Daylight Time, UTC-07:00)
But how to add the object id to the tracing output, e.g. to help debug timing dependent behavior?
Anonymous
Comments are closed.