How to set conditional breakpoints in Visual Studio?

前端 未结 13 2330
忘了有多久
忘了有多久 2020-11-27 03:35

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

13条回答
  •  遥遥无期
    2020-11-27 04:15

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

提交回复
热议问题