Introduction
Sometimes during debugging of your code, you may be in part
of code that is called deep within nested functions and you figure that there
is a certain object that is causing the issue. In order to verify your theory,
you may need to instantiate the same object with some new values. In a typical debugging
session, you will change the values of that object wherever its instantiated
but this will require you to stop debugging session, make code changes and then
start debugging session again. However there is a quicker way of doing it also
without stopping your debugging session. Let’s say you have a Point
class with
an overloaded +
operator as shown below:
class Point
{
public int X { get; private set; }
public int Y { get; private set; }
public Point(int x, int y)
{
this.X = x;
this.Y = y;
}
public static Point operator +(Point P1, Point P2)
{
return new Point(P1.X + P2.X, P1.Y + P2.Y);
}
}
Let’s say you are using this class as follows:
class Program
{
static void Main(string[] args)
{
Point P1 = new Point(10, 20);
Point P2 = new Point(90, 70);
Point P3 = P1 + P2;
}
}
Let’s say there is something wrong in the overloaded +
operator implementation. In order to figure out what the problem is, you set a
breakpoint with the + operator. When this breakpoint gets hit, you figure that
the problem only happens with P1
object has negative values for X
and Y
.
However, as shown in figure below in current debugging session, you have positive
values for X
and Y
.

As mentioned earlier, if you want to test this function with
negative values for P1
object, typically you will stop your debugging session,
go back in the Main
method and modify the code with the following:
Point P1 = new Point(-10, -20);
Alternatively, you can
achieve the same thing without leaving your current debugging session. In order to
do that, first you will instantiate a Point
Object in your Immediate
Window.
If you haven’t used this window before,
this can be brought into view by Debug -> Windows -> Immediate menu
option as shown below:

Once you get to immediate window, instantiate a Point
object as shown below:

Once this object is instantiated, you will see that this
variable name Pt
also becomes available in Local Variables windows. You will
see that the name of variable will be prefixed with a $
symbol, which basically implies that it’s
a compiler generated variable.

Next, you assign this Pt
variable to P1
in immediate window. As soon as you assign this new variable to P1
, you will see that P1
variable will get updated in Locals window. In fact, values of P1.X
and P1.Y
will be shown in red which indicates that these values have been changed as shown below:

This way, you were able to modify P1
object without stopping your debug session. I hope you find this tip helpful.