Debugging Tips using Visual Studio


Hi folks, today I will share some debugging tips which many of you might be knowing already but this is for who not know the full capabilities of the Visual Studio Editor and are missing out these features while doing their development activities.

Many a times while writing/analysing code we come across scenarios where one wants to break a certain piece of code when certain condition is met, one of the methods that comes first is to put a break point and wait till that condition is met.

Let us see how we can achieve conditional debugging in Visual Studio:

I have taken simple console based application and written a small piece of code that simulates a long for loop:

for (int i = 0; i < 20; i++)
{
    Console.WriteLine("i : {0} ", i);
}
 
Console.Read();



Now put a debug point at line that reads Console.Writeline(), and right click the debug point as shown below in the image:

Debugging1.gif

As you can see there are many options at her hand to play round with, but let us take the simplest of the way around and choose the "Condition…" option and you will get the following pop-up window:

Debugging2.gif

Next we can go ahead and specify the condition for the debug point to hit in the textbox available (one of the cool things is that you will get the intelisense feature here too and you can access all the variables defined in the scope:

Debugging3.gif

As you can see I can access my local variable which I defined earlier in our code sample, going ahead let us specify that the debug point hits only when the value of i is equal to 6.

Debugging4.gif

After we are done with condition specification, press ok and you will see a white mark on debug point which specifies that the debug is a not a normal debug point but comes with some condition specified over it.
Now run you solution by pressing F5, and you will that the debug point is hit only when I becomes 6 in the loop.

Debugging5.gif

You can verify the same in the Locals window the value of the variable. This is indeed a valuable feature to have a big set of values to debug out with a specific value.

We will continue our journey more on the same, till then have fun.

Cheers, 

Next Recommended Readings