How to set conditional breakpoints in Visual Studio?

C#.NetVisual StudioBreakpoints

C# Problem Overview


Is there an easy way to set conditional breakpoints in Visual Studio?

If I want to hit a breakpoint only when the value of a variable becomes something, how can I do it?

C# Solutions


Solution 1 - C#

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

Solution 2 - C#

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.

Solution 3 - C#

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)

Solution 4 - C#

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

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

Solution 5 - C#

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

Solution 6 - C#

Writing the actual condition can be the tricky part, so I tend to

  1. Set a regular breakpoint.
  2. Run the code until the breakpoint is hit for the first time.
  3. Use the Immediate Window (Debug > Windows > Immediate) to test your expression.
  4. Right-click the breakpoint, click Condition and paste in your expression.

Advantages of using the Immediate window:

  • It has IntelliSense.
  • You can be sure that the variables in the expression are in scope when the expression is evaluated.
  • You can be sure your expression returns true or false.

This example breaks when the code is referring to a table with the name "Setting":

table.GetTableName().Contains("Setting")

Solution 7 - C#

Create a breakpoint as you normally would, right click the red dot and select "condition".

Solution 8 - C#

On Visual Studio 6.0

Alt+F9!!!

Solution 9 - C#

  1. Set a breakpoint as usual
  2. Right click on the breakpoint and select Condition
  3. You'll see a dialog that says "Breakpoint Condition"
  4. Put a condition in the field e.g. "i==5"

The breakpoint will only get hit when i is 5.

Solution 10 - C#

  1. Set a breakpoint as usual.
  2. Right-click on the breakpoint marker
  3. Click "Condition..."
  4. Write a condition, you may use variable names
  5. Select either "Is True" or "Has Changed"

Solution 11 - C#

Create a conditional function breakpoint:

  1. In the Breakpoints window, click New to create a new breakpoint.

  2. On the Function tab, type Reverse for Function. Type 1 for Line, type 1 for Character, and then set Language to Basic.

  3. Click Condition and make sure that the Condition checkbox is selected. Type instr.length > 0 for Condition, make sure that the is true option is selected, and then click OK.

  4. In the New Breakpoint dialog box, click OK.

  5. On the Debug menu, click Start.

Solution 12 - C#

Set the breakpoint as you do normally, right click the break point and select condion option and sets your condition.

Solution 13 - C#

If you came from Google, this answer might be what you are searching for.

  1. Click Debug> New BreakPoint > Function Breakpoint enter image description here

  2. there choose the conditional Breakpoint.

Solution 14 - C#

You can control when and where a breakpoint executes by setting conditions. The condition can be any valid expression that the debugger recognizes. For more information about valid expressions, see Expressions in the debugger.

To set a breakpoint condition:

  1. 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.

    You can also set conditions in the Breakpoints window by right-clicking a breakpoint and selecting Settings, and then selecting Conditions.

enter image description here

  1. In the dropdown, select Conditional Expression, Hit Count, or Filter, and set the value accordingly.

  2. Select Close or press Ctrl+Enter to close the Breakpoint Settings window. Or, from the Breakpoints window, select OK to close the dialog.

Breakpoints with conditions set appear with a + symbol in the source code and Breakpoints windows.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMrClanView Question on Stackoverflow
Solution 1 - C#JeffView Answer on Stackoverflow
Solution 2 - C#panakoView Answer on Stackoverflow
Solution 3 - C#C JohnsonView Answer on Stackoverflow
Solution 4 - C#Ignacio Soler GarciaView Answer on Stackoverflow
Solution 5 - C#TigranView Answer on Stackoverflow
Solution 6 - C#Andy JoinerView Answer on Stackoverflow
Solution 7 - C#Brandon MoretzView Answer on Stackoverflow
Solution 8 - C#VoracView Answer on Stackoverflow
Solution 9 - C#user423119View Answer on Stackoverflow
Solution 10 - C#CaseyView Answer on Stackoverflow
Solution 11 - C#JAiroView Answer on Stackoverflow
Solution 12 - C#FIre PandaView Answer on Stackoverflow
Solution 13 - C#T.ToduaView Answer on Stackoverflow
Solution 14 - C#Vijay KumavatView Answer on Stackoverflow