Debug/Breakpoints With Conditions In C#

Debugging is a very important aspect of programming, but debugging with conditional breakpoints will increase your performance and you can also validate your data during this process. Breakpoints are one of the most important debugging techniques in your developer's toolbox. We set breakpoints wherever we want to pause debugger execution to see the state of code variables or look at the call stack at a certain point through breakpoints. We can also use breakpoint when we are trying to resolve a warning or issue in the program.

How to set breakpoints in the source code

We can set a breakpoint on any line of executable code to pause the debugger to see some values. For example, in the following C# code, you could set a breakpoint on the line of code with the variable assignment, the for loop, or any code inside the for loop. You can't set a breakpoint on method signatures, declarations for a namespace or class, or variable declarations if there's no assignment and no getter/setter.

Debug/Breakpoints With Conditions In C#

To set a breakpoint in source code, click in the far-left margin next to a line of code. You can also select the line and press F9, select Debug -> Toggle Breakpoint, or right-click and select Breakpoint -> Insert breakpoint. The breakpoint appears as a red dot in the left margin.

In the above code snippet, you have a breakpoint and when you reach on line 237, debugger will stop to check the value in the string variable and highlighted as yellow which shows a breakpoint hit. As shown below,

Debug/Breakpoints With Conditions In C#

Conditional Breakpoint

You can control the breakpoint through certain conditions. Breakpoint will hit if those conditions or criteria are met but those conditions should be a valid expression or an understandable format of a debugger.

Debug/Breakpoints With Conditions In C#

Right-click the breakpoint symbol and select Conditions. Or hover over the breakpoint symbol, select the Settings icon, and then select Conditions in the Breakpoint Settings window.

Debug/Breakpoints With Conditions In C#

After selecting the above option you will see the following windows to add conditions.

Debug/Breakpoints With Conditions In C#

You can specify the conditions as much as you can, you can also specify their respective actions. Actions will be shown in the Output screen.

The beauty is, when you apply any condition, you will see all the variables and other resources in the dropdown. Let’s add a condition to demonstrate this feature,

Debug/Breakpoints With Conditions In C#

I have added a condition that breakpoint will hit when value = “Corner” but in our case value should be “C-Sharp” after substring evaluation. So breakpoint will not hit on this stage and point to the next break.

Debug/Breakpoints With Conditions In C#

As per the below snippet, you will see our first breakpoint will not hit and directly stop on line 240. Once we change our condition like value == “C-Sharp” then it will hit.

Debug/Breakpoints With Conditions In C#

Hit Count: We can also apply a condition on loop that when a specific counter is met,  then hit the breakpoint. This will be very productive when we have a huge amount of data and want to debug on a certain count.

Debug/Breakpoints With Conditions In C#

In our scenario, breakpoint will hit when loop condition i == 3

Debug/Breakpoints With Conditions In C#

You can read more from here about expressions used in debugger.

https://docs.microsoft.com/en-us/visualstudio/debugger/expressions-in-the-debugger?view=vs-2022

How to Remove/Disable Breakpoint

You can either remove or disable the breakpoint if not needed anymore.

The breakpoint is a toggle. You can click it, press F9, or use Debug -> Toggle Breakpoint to delete or reinsert it.

Without deleting, if you want to disable it, hover or right-click it, and select Disable breakpoint. If you want to reverse/re-enable it, again right click on the breakpoint and enable it.

Debug/Breakpoints With Conditions In C#

Conclusion

In this article, we have learned about breakpoint, conditional breakpoint with their different options and how it is productive for the developer. How to disable/delete when not required. Happy Coding!! 😊


Similar Articles