Monday, November 9, 2009

Using DebuggerStepThrough Attribute

When debugging code, one of the annoying things is to step into an one-line method or property. Assume that you have the following property:

private string word;
public string Word {
get { return word; }
set { word = value; } }

And you have a code that uses that property when calling a method:

DoSomething(obj.Word);

When you debug that line, and hit F11 to step into the method, you'll step into the get section of the property, and only then move on to the method.

By placing System.Diagnostics.DebuggerStepThrough attribute above get and set sections of the property you instruct the debugger to step through that property and not into it:

public string Word {
[System.Diagnostics.DebuggerStepThrough]
get { return word; }

[System.Diagnostics.DebuggerStepThrough]
set { word = value; } }

This instruction will cause the debugger not to step into method (property) as normal, but you can always place a breakpoint in that method and stop there.