Alex
Alex

Reputation: 2609

Can I make my C# code stop at breakpoint when Visual Studio debugger is evaluating the value?

I can't figure out how to catch the moment when Visual Studio debugger is evaluating my property value in C# code. Breakpoints only work if my code is accessing the property, not Visual Studio debugger (when this property is watched).

My question is: does Visual Studio have a setting or feature which changes the behavior of breakpoints so they become hit (if such breakpoint is located in getter of a property) when Visual Studio debugger shows the value of this property in Watch window (and somehow executing this getter for that)?

I need this for solving my issue (but the issue is NOT my question, it's just to provide some background why I ever needed the feature I'm asking for): for some reason, somehow reading some property of my object by the debugger makes unwanted side effects (causing another property to change). When I do the same in the code (read the properties of the object), nothing like that happens. To locate which property (as I have many dozens of them) causes this effect, I would like to be make breakpoints being hit when debugger evaluates expressions causing my code with these breakpoints to execute.

I couldn't find any feature to enable this in Visual Studio. Am I missing something? Or, maybe, it was added later? I'm on Visual Studio 2008 now.

EDIT: I got downvotes so I need to clarify it again. I'm not seeking assistance with finding the problem in my code (that's why no code here). I'm asking for a very concrete thing. Can I make Visual Studio stop on breakpoints when execution occurred due to evaluating some expression in the debugger. Just that.

public int MyProp1
{
    get
    {
      DoSomething1(); // I want VS debugger stop here on evaluating expression in Watch window
      return _value1;
    }
}
...
public int MyProp99
{
    get
    {
      DoSomething99(); // I want VS debugger stop here on evaluating expression in Watch window
      return _value99;
    }
}

Upvotes: 0

Views: 1422

Answers (5)

Aaa
Aaa

Reputation: 910

While you can't hit a breakpoint when an expression is typed into the Watch panel, you can achieve this functionality by typing the expression into Visual Studio's immediate window. Any set breakpoint will be hit. This is applicable to Visual Studio 2022.

Upvotes: 1

Mohammad Komaei
Mohammad Komaei

Reputation: 9676

Right click on your break point for example in a for loop then click Condoitions , then type your condition for example i>5 or i==5 then click Close button. enter image description here

Upvotes: -1

A Friend
A Friend

Reputation: 41

You can't enable breakpoints during debugger property evaluation. What you can do, if the evaluation side effects are causing you debugging issues, is turn off property evaluation and other implicit calls (like calls by the debugger to ToString())

Disable it under Debug->Options->General->Enable property evaluation and other implicit function calls.

Upvotes: 4

Jack Zhai
Jack Zhai

Reputation: 6436

Like the document which share us how we could use the breakpoint in VS IDE:

https://msdn.microsoft.com/en-us/library/5557y8b4(v=vs.110).aspx

My understanding is that the data breakpoint could achieve the function you want o get, but this feature really has a limitation now, it just supports the VC++ native now.

So I agree with Brian Reynolds, the workaround is that you could try to break on or intercept Add and Remove from the debugging watch window.

Of course, for the data breakpoint issue, other community members also submitted this feature question to the product team here:

https://visualstudio.uservoice.com/forums/121579-visual-studio-ide/suggestions/6097301-support-data-breakpoints-for-c

You could also vote it or submit your feature request if you have any good suggestion for the VS product.

Upvotes: -1

Brian Reynolds
Brian Reynolds

Reputation: 25

You can set up conditional breakpoints in VS. Set the breakpoint on the line you wish, right click and hit condition.

This link can help you out with breakpoint conditions: https://blogs.msdn.microsoft.com/zainnab/2010/05/03/set-a-simple-breakpoint-condition/

Upvotes: 0

Related Questions