Conditional Debugging in Visual Studio 2015

Content

Debugging is a tool that is very useful for every developer. Developers can't even think of programming without debugging in this era.

In debugging, you just put the breakpoint anywhere in your code and check the value using the "immediate window" or hover the mouse over a variable/object. You also use F10/F11 to move forward.

Why we need Conditional Debugging

Just take a real scenario where you wrote some code in a loop that executes 10 times but for the 9th iteration something unwanted happens like an error, wrong result, exception and so on then what will you do?

For the earlier conditional debugging feature, to reach the 9th iteration you use F5 from iteration 1 to iteration 9 but using conditional debugging you can easily get directly to the 9th iteration.

To explain it visually see the following code:

visual studio

Open Visual Studio 2015 and select "File" -> "New" -> "WebSite" and fill in the WebSite name as "WebSite1".

WebSite

After creating the WebSite, I will create a Page named "Default.aspx".

web form

Now I will create a "for" loop that will execute 10 times.

  1. protected void Page_Load(object sender, EventArgs e)  
  2. {  
  3.   
  4.    for (int i = 1; i <= 10; i++)  
  5.    {  
  6.       //Do the code  
  7.    }  
  8. }  
cs code

1. Now I will put a breakpoint on the left side.

2. Then click on the "settings" icon to specify the condition.

breakpoint

A popup window named "Breakpoint Settings" will open like:

Breakpoint Settings

Click on the check box of Conditions, set the condition using an expression like i==9 and press Enter.

Conditions

When I run the page using F5, the breakpoint will only be hit for the true condition and that is i==9.

true condition

As you can see in the preceding image, the breakpoint works on the 9th iteration.

Conclusion

Now you understand about the Conditional debugging and I hope you will use it when debugging.


Similar Articles