Uniquely Identifying Reference Types in the Debugger

前端 未结 4 776
自闭症患者
自闭症患者 2020-12-01 13:24

I come from a C++ background, so apologies if this is a non-C# way of thinking, but I just need to know. :)

In C++ if I have two pointers, and I want to know if they

4条回答
  •  一个人的身影
    2020-12-01 14:04

    What you're looking for are object id's. For any referenc type in the debugger you can right click and say "Make Object ID". This will add a # suffix to the value column whenever that instance is displayed in the debugger. You can also add #1, #2, etc ... to the watch window to see them again any time later.

    Step 0 - Run this code

    static void Main(string[] args)
    {
        var x = "a string";
        var y = x;
        System.Diagnostics.Debugger.Break();
    }
    

    Step 1 - Right Click and select "Make Object Id"

    alt text

    Step 2 - Instances now display with the 1# suffix. Note: I did nothing special in this step. Immediately after clicking "Make Object Id" both rows updated to display the 1# suffix since they refer to the same instance.

    alt text

    Step 3 - See them at any time by adding 1# to the watch window

    alt text

提交回复
热议问题