Dutton

How to uniquely identify an object when debugging C# in Visual Studio

Many years ago I used to develop applications in C++ under Visual Studio and despite thoroughly loving the move to managed code, in particular C# .Net I do find that I occasionally miss some of the ways I used to be able to debug old non-managed programming languages.

One thing in particular I miss when trying to fix a frustratingly elusive bug (normally involving threads or some other equally non-determinstic nightmare ;) ) is the ability to see the memory address of a particular object instance on the heap under the debugger and compare that to an instance where it appears elsewhere in my execution to be sure that they are the same.

Up to now I have just written that off as one of those managed code limitations and worked around it. That was until a colleague (cheers Dan) pointed out something that's been under my nose this whole time, this:

Make Object ID

In the Locals or Watch windows when debugging, you can right-click on any reference type and "Make Object ID" which will append a number (followed by a '#' surrounded by curly braces) to the end of the value like this:

Object ID

This will uniquely identify the object until you either right-click again and select "Delete Object ID" or stop debugging.

And here's its MSDN description (under Managed Object Identity).

I swear I really should RTFM when it comes to Visual Studio. I hoped that this was something new in VS2012 but I fired up an old VS2010 project and there it is again! It's been under my nose this whole time!


Share this: