class Person
{
public Person(string name)
{
Name = name;
}
public string Name { get; set; }
}
public class Program
{
public static void Main(string[] args)
{
DoIt();
}
private static void DoIt()
{
var p = new Person("Baldrick");
DoIt2();
}
private static void DoIt2()
{
return;
}
}
If you step through this code in Visual Studio and add a watch to variable p in method DoIt, the watch window will look like this:
When you step into method DoIt2, or out to Main, and refresh the watch on p, you get this message in the watch window:
If you want to be able to reliably watch the variable, the solution is to Make Object ID. Let's take the code back to DoIt and right-click on the watch on variable p:
Click on Make Object ID, and you'll see variable p is allocated object ID "1#"
If you add a watch to 1#, this allows you to view the value of p even when p goes out of scope:
The 1# reference can be used to update the value of the underlying variable p:
which will be reflected when (if) the object comes back into scope:
What about Garbage Collection?
The Object ID doesn't create a managed reference to the underlying object, which means that it will not interfere with its garbage collection:









No comments:
Post a Comment