BExner
BExner

Reputation: 71

C#: VS 2008 debugger executing property code

I have the following 2 lines of code:

lstvbWerteC.Clear ();
if (...)

lstvbWerteC is a field of List<T> with class scope. If in the VS 2008 debugger I set a breakpoint on the if statement I would expect lstvbWerteC.Count to be 0, but instead it is 1. My class has some properties that indeed fill the list. To prevent side effects in the debugger all the properties have the attribute [DebuggerBrowsable (DebuggerBrowsableState.Never)]. Nevertheless as soon as the locals window is visible the debugger seems to execute the code filling the list. With the locals window hidden the list is always empty reaching the if-breakpoint. Any information on debugger details concerning this type of problem would be appreciated.

Upvotes: 5

Views: 166

Answers (2)

Alex F
Alex F

Reputation: 43331

Visual Studio menu: Tools - Options - Debugging - General. Uncheck "Enable property evaluation" option.

This is from Visual Studio 2010. Visual Studio 2008 should have the same option or something similar.

Upvotes: 5

Dan Puzey
Dan Puzey

Reputation: 34218

I would imagine that the behaviour you describe is undefined in most documentation. Certainly there's no requirement than any debugger (VS included!) should honour the DebuggerBrowsable attribute. Further, the lack of display of an item in a debug window doesn't necessarily guarantee that the item isn't evaluated by the debugger.

There's an option under the debugging options called "Enable Property Evaluation" that may or may not help your specific case, but in general I'd say you can't rely on this.

There's a commonly-repeated piece of advice: "property getters should not have [visible] side-effects." Your property getters do have side-effects, and that's causing problems with your debugging experience, but it is also possibly making your class as a whole less understandable for other developers. My advice would be to consider a redesign such that getting a property value doesn't alter the content of the list.

Upvotes: 1

Related Questions