Hit Conditional Breakpoints in C#

Previously in our discussion, we discussed the concept of Conditional breakpoints. This article explains Hitting Conditional breakpoints.

Consider a case where you have a for loop iterating through a thousands records. You know that your records at around the 500th location is corrupted and you need to debug that value. Will you sit and continue to press F5 until the record number 500 is reached ? This is where you can use a conditional breakpoint. This is done by setting the Hit Count condition on the breakpoint, in other words hit the breakpoint when has it iterated 499 times.

Let's create a sample application to understand this. Add a for loop with an iteration of 1000 records and add a condition to hit the breakpoint when the 500th record is being iterated.

To add the condition, right-click on the breakpoint and select the 'Hit Count' condition. This will open a window where we can define a condition to hit the breakpoint only when it meets the criteria set for its execution.

hit count

Here we have 4 conditions and they are self explanatory. So we select the condition "break when the hit count is equal to" and set its value to 500.

break when the hit count is equal to

Hover to the breakpoint and see the details:


Run the application and see the value of variable 'i', when the breakpoint is hit.

Run the application

So here when the value of i becomes 499 (starting from 0), it is iterating the 500th record and the breakpoint is hit. Set the breakpoint and enjoy debugging!

Similar Articles