Conditional Breakpoints in C#

In this article you will learn how to use Conditional Breakpoints in the C# language.

This is just a simple trick that many of you might have been using. It's a small but very powerful tip, especially helpful when you are debugging a large code base. This is the concept of Breakpoint Conditions.

As the name itself suggests, it is setting the breakpoints that are hit, only when a certain condition is met. It's easy to implement as well. Create a new application and set a normal breakpoint.

normal breakpoint

So this will be hit every time the Page_Load executes. This could be very annoying when you are debugging and it continues to hit this breakpoint. So let's make it conditional. Simply put your mouse on the breakpoint, right-click and select "Condition".

condition

This will open up a window where we can define a condition to hit the breakpoint only when the condition is met. You can even get the intelligence (Intellisense?) help for it.
isPostBack

See how the color of the breakpoint changes with a plus sign on it. Also, hover over it and you can see the details.

color of the breakpoint

Now let's run the application. The first time, no breakpoint is hit. Press the button for postback and see the breakpoint breaks the execution.

run the application

You can even set this condition to be on your variables or properties and so on. So happy debugging now!