GazTheDestroyer
GazTheDestroyer

Reputation: 21241

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?

Upvotes: 9

Views: 8006

Answers (2)

Robbie
Robbie

Reputation: 19500

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.

Upvotes: 18

dmytro.s
dmytro.s

Reputation: 720

object.ReferenceEquals(objA, objB)

Upvotes: 6

Related Questions