-1

how do I know during debugging whether obj and obj2 reference the same object in the following code?

object obj = new object();
object obj2 = new object();
obj2 = obj;

In VS 2008 I could easily see that by looking in the watch window or locals window or on the value tooltip of the variable, because they all showed the object address, which was really useful (code sample is C++/CLI, but was tested in C# as well):

enter image description here

But VS 2015 does not. How do I get that info here?

Tobias Knauss
  • 3,361
  • 1
  • 21
  • 45
  • I'm using GetHashCode() for that, 2 different instances usually gives different hashcodes, but it can be tricky. – 3615 Jun 12 '17 at 06:37
  • 1
    @3615: unsafe. Instances may be different but contain same data. – Tobias Knauss Jun 12 '17 at 06:38
  • All you have to do is use the standard mechanism for testing for reference equality (object identity). See marked duplicate for specifics. Use the immediate window to make the actual call, passing the two references you want to check. – Peter Duniho Jun 12 '17 at 06:42
  • 1
    @PeterDuniho That's even more laborious. I prefer the solution that I found myself and posted below. Also, the "duplicate" post is everything but a duplicate! If you want to mark my question as a duplicate, then use the link I gave in my answer! – Tobias Knauss Jun 12 '17 at 06:46
  • _"use the link I gave in my answer!"_ -- I did. But your question is also a duplicate of the other: i.e. you want to know how to test if two references are equal. That's answered precisely by the first as well. If you have some additional criteria that causes you to believe that perfectly good answers aren't adequate, it's your responsibility to make that clear in your question. Nothing in your question suggests anything except a desire to make the determination of reference equality. – Peter Duniho Jun 12 '17 at 06:49
  • 1
    Just keep the Debug > Windows > Memory > Memory 1 debugging window handy. It shows the address. – Hans Passant Jun 12 '17 at 07:10
  • I understand your pain, I also wish it was easier to just immediately see the unique address of the object. So far the easiest way I've found is to just do "Add Watch" on all the objects I'm interested in inspecting, then adding the object ID's from the Watch menu. – Shahin Dohan Dec 02 '20 at 13:54

1 Answers1

0

After further searching and testing I found
Uniquely Identifying Reference Types in the Debugger
which gives a good workaround by creating "object IDs". However, in my opinion this is just a workaround and I would prefer if I could see the required info instantly.

Tobias Knauss
  • 3,361
  • 1
  • 21
  • 45
  • I don't understand how making an object ID is "a workaround". As soon as you make the object ID, you'll see the required info "instantly", just like you want. I note that the post you found even mentions that you can just call `object.ReferenceEquals()` in the immediate window, just like you'd do any time you want to test for reference equality in code. So there's that option too. – Peter Duniho Jun 12 '17 at 06:45