[c#] How to set conditional breakpoints in Visual Studio?

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?

This question is related to c# .net visual-studio breakpoints

The answer is


  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"

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

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.


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)

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.


  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.


On Visual Studio 6.0

Alt+F9!!!


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.


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


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


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")

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

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

Examples related to c#

How can I convert this one line of ActionScript to C#? Microsoft Advertising SDK doesn't deliverer ads How to use a global array in C#? How to correctly write async method? C# - insert values from file into two arrays Uploading into folder in FTP? Are these methods thread safe? dotnet ef not found in .NET Core 3 HTTP Error 500.30 - ANCM In-Process Start Failure Best way to "push" into C# array

Examples related to .net

You must add a reference to assembly 'netstandard, Version=2.0.0.0 How to use Bootstrap 4 in ASP.NET Core No authenticationScheme was specified, and there was no DefaultChallengeScheme found with default authentification and custom authorization .net Core 2.0 - Package was restored using .NetFramework 4.6.1 instead of target framework .netCore 2.0. The package may not be fully compatible Update .NET web service to use TLS 1.2 EF Core add-migration Build Failed What is the difference between .NET Core and .NET Standard Class Library project types? Visual Studio 2017 - Could not load file or assembly 'System.Runtime, Version=4.1.0.0' or one of its dependencies Nuget connection attempt failed "Unable to load the service index for source" Token based authentication in Web API without any user interface

Examples related to visual-studio

VS 2017 Git Local Commit DB.lock error on every commit How to remove an unpushed outgoing commit in Visual Studio? How to download Visual Studio Community Edition 2015 (not 2017) Cannot open include file: 'stdio.h' - Visual Studio Community 2017 - C++ Error How to fix the error "Windows SDK version 8.1" was not found? Visual Studio Code pylint: Unable to import 'protorpc' Open the terminal in visual studio? Is Visual Studio Community a 30 day trial? How can I run NUnit tests in Visual Studio 2017? Visual Studio 2017: Display method references

Examples related to breakpoints

Bootstrap 3 breakpoints and media queries How to set a JavaScript breakpoint from code in Chrome? How to set conditional breakpoints in Visual Studio? How to use breakpoints in Eclipse How do I remedy "The breakpoint will not currently be hit. No symbols have been loaded for this document." warning? Eclipse - Unable to install breakpoint due to missing line number attributes Break when a value changes using the Visual Studio debugger Why aren't Xcode breakpoints functioning? Can I set a breakpoint on 'memory access' in GDB?