Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any way to show/compare object references in watch window?

When debugging I sometimes find the need to compare object references in the watch window to see if two variables are actually referencing the same object.

With C++ and pointers this is easy, but is there any way of doing this with C# references?

like image 568
GazTheDestroyer Avatar asked Apr 11 '12 10:04

GazTheDestroyer


People also ask

How do I compare two objects in PowerShell?

The Compare-Object cmdlet compares two sets of objects. One set of objects is the reference, and the other set of objects is the difference. Compare-Object checks for available methods of comparing a whole object.

How do you compare objects?

Whereas the equals() method compares two objects. Objects are equal when they have the same state (usually comparing variables). Objects are identical when they share the class identity. For example, the expression obj1==obj2 tests the identity, not equality.

How do I show autos in Visual Studio?

To open the Autos window, while debugging, select Debug > Windows > Autos, or press Ctrl+Alt+V > A. To open the Locals window, while debugging, select Debug > Windows > Locals, or press Alt+4. This topic applies to Visual Studio on Windows.


2 Answers

There is actually a built in feature for comparing objects in the Watch window that doesn't require you to call any functions directly. It's in the right click menu as "Make Object ID"

Make Object ID

It will mark the object with an ID, and then you can add a second object and mark it with an id as well. If those object are the same reference, then they will have the same ID. This allows you to see if/when they change as you are debugging.

like image 192
Robbie Avatar answered Oct 16 '22 16:10

Robbie


object.ReferenceEquals(objA, objB)
like image 26
dmitry.s Avatar answered Oct 16 '22 16:10

dmitry.s