Reputation: 34297
When debugging, I was expecting two different classes to be using the same instance of an object. All of the properties were the same for these two objects, but they were two different instances. Is there a way to tell that in the VS debugger?
In order to tell for sure, I was able to add a field to the class:
private string someId = Guid.NewGuid().ToString();
Then, when debugging, I could at least look at that field for each of the two instances. Is there a better way that wouldn't involve having to create this dummy ID field?
Upvotes: 29
Views: 12261
Reputation: 1484
In addition to the answers above, you can also compare pointers. If you have two objects obj1
and obj2
, you can check (e.g. in Watch Window) their addresses with: &obj1
and &obj2
.
Note that objects may be moved around by the .NET runtime, so two address checks of the same object separated with some code execution in between may result in different addresses.
Upvotes: 2
Reputation: 3490
When debugging, in the Locals window, right-click on the instance and select "Make Object ID".
This will add number that is unique for this instance which is displayed whenever you see this instance in the debugger (in tool-tips as well as in the watch window).
Upvotes: 56
Reputation: 22569
Object.Equals Method (Object, Object)
Edit: To check reference equality use ReferenceEquals
Edit 2: While Debugging, Go to debug menu, windows --> immediate window (intellisense should work here) and ?Object.ReferenceEquals(obj1, obj2)
Upvotes: 5