How to set conditional breakpoints in Visual Studio?

Set a breakpoint as usual. Right click it. Click Condition.


When you are using Express edition you can try this:

#if DEBUG
    if( fooVariable == true )
        System.Diagnostics.Debugger.Break();
#endif

if statement makes sure that in release build breakepoint will not be present.


Visual Studio provides lots of options for conditional breakpoints:

To set any of these you

  1. Set a breakpoint.
  2. Right-Click over the breakpoint, and in the popup menu you select an option that suites you.

These options are as follows:

  • You can set a condition, based on a code expression that you supply (select Condition from the popup menu). For instance, you can specify that foo == 8 or some other expression.
  • You can make breakpoints trigger after they have been hit a certain number of times. (select Hit Count from the popup menu). This is a fun option to play with as you actually aren't limited to breaking on a certain hit count, but you have options for a few other scenarios as well. I'll leave it to you to explore the possibilities.
  • You can Set filters on the Process ID, thread ID, and machine name (select Filter from the popup menu)

Just another way of doing it, (or if you are using express) add the condition in code:

if(yourCondition)
{
    System.Diagnostics.Debugger.Break();
}

  1. Set breakpoint on the line
  2. Right clik on RED ball
  3. Chose conditioal breakpoint
  4. Setup condition