Is there a way to enter debug mode when a certain condition is met?
For example let's say I would want to enter debug mode on the line on which i == 1
becomes true:
using System;
namespace ConditionalDebug
{
public class Program
{
public static void Main(string[] args)
{
var r = new Random();
var i = r.Next(2);
i += r.Next(2);
i += r.Next(2);
i += r.Next(2);
i += r.Next(2);
i = 1;
Console.WriteLine(i);
}
}
}
I know it is possible to set conditional breakpoints like:
But of course I couldn't use that since I would have to add a conditional breakpoint for each line in the code where the condition value might get changed and that would get very messy in a real application.
So, is there a way to globally set the condition i == 1
so that the debugger will break on the line on which the condition is met?
Thanks for your help!
Conditional breakpoints allow you to break inside a code block when a defined expression evaluates to true. Conditional breakpoints highlight as orange instead of blue. Add a conditional breakpoint by right clicking a line number, selecting Add Conditional Breakpoint , and entering an expression.
Run the program in debug modeClick the Run icon in the gutter, then select Modify Run Configuration. Enter arguments in the Program arguments field. Click the Run button near the main method. From the menu, select Debug.
Right-click the breakpoint symbol and select Conditions (or press Alt + F9, C). Or hover over the breakpoint symbol, select the Settings icon, and then select Conditions in the Breakpoint Settings window.
A conditional breakpoint insertion can be done by using one single command by using the breakpoint command followed by the word if followed by a condition. In the previous example, the breakpoint can be inserted with the line b 29 if (y == 999).
The short answer is 'No'
The long answer is 'Not really, but kinda'. There are things you can do to get close to the behaviour you want.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With