.Net Tips — use DebuggerStepThrough attribute

When debug­ging your appli­ca­tion, don’t you just get annoyed that press­ing F11 keep tak­ing you into the get method of the prop­er­ties (auto-prop­er­ties exclud­ed!) passed into the method rather than the method you want to step into?

I use auto-prop­er­ties wher­ev­er pos­si­ble because they’re syn­tac­ti­cal­ly clean­er and have less main­te­nance over­head, and I have the same amount of con­trol as with pri­vate fields because I can define the access priv­i­lege of the get and set meth­ods inde­pen­dent­ly.

The only time when I will still use a pri­vate field is when I want to expose a read­on­ly vari­able so I can con­fine the assign­ment to the con­struc­tor but still allow oth­er class­es to access it. 99.9% of the time the get method will do a straight return so step­ping through it have no ben­e­fit what­so­ev­er, and it’s times like this the Debug­ger­StepThrough attribute can be a time­saver:

private int _myIntField;
public int MyIntProperty
{
    [DebuggerStepThrough]
    get { return _myIntField; }
}

This will stop the debug­ger from step­ping into the get method when you try to step into a method like this:

DoSomething(obj.MyIntProperty);

You can, how­ev­er, still put a break point against the get method when you do want to step through it with­out hav­ing to first take off the attribute and recom­pile though! :-D

Anoth­er sit­u­a­tion where the Debug­ger­StepThrough attribute can save you time is when you use Post­Sharp-pow­ered attrib­ut­es.

When you’re debug­ging through some code which has try-catch log­ic encap­su­lat­ed in a OnMethod­In­vo­ca­tionAspect attribute like the RetryOn­SqlDead­Lock­O­r­Con­nec­tion­Time­Ou­tEx­cep­tion­At­tribute I have here, you might be tak­en into attribute class when all you care about is what’s hap­pen­ing in the method the attribute is applied to.