I have a bunch of classes that implement cached properties that return collections of data retrieved from a database. The problem I have is if I set breakpoints when debugging VS 2010 appears to execute the property itself in order to display the count of dictionary items.
How can I stop VS from executing the property before it’s ready?
Thanks in advance…
Example that shows the problem:
public class CTest
{
private ICollection<int> _col = null;
public ICollection<int> col
{
get
{
if (this._col == null)
{
System.Diagnostics.Debug.Assert(false, "ASSERT!");
this._col = new Collection<int>();
this._col.Add(1);
this._col.Add(2);
this._col.Add(3);
}
return this._col;
}
}
}
CTest test = new CTest();
// A breakpoint on this line and no assert will fire
int nCount = test.col.Count;
// A breakpoint on this line and assert will fire
nCount = test.col.Count;
This is caused by automatic property evaluation. The general guidelines for .NET development state that property evaluation should be fast and not cause side-effects. Obviously, things like caching and lazy loading in ORM’s violate this principal in favor of increased usability, but what you’re experiencing is one consequence of this violation.
To counter this, you need to turn off automatic property evaluation in the debugger options dialog box. See this MSDN link for more information.
(On an entirely unrelated note, standard .NET conventions call for pascal-cased public members such as properties and functions. Consider capitalizing your property name [
Dicinstead ofdic] and giving it a more descriptive name).