COTW: DebuggerStepThroughAttribute Class
The DebuggerStepThroughAttribute[^] class allows you to specify classes, structs, constructors, properties, and methods that you do not want the debugger to step into. Unlike DebuggerHiddenAttribute[^], which we saw last week[^], you can still set break points in items that have DebuggerStepThroughAttribute specified and Visual Studio will not disable them.
This gives you the benefit of automatically stepping over items, while still allowing you to debug the item if needed. If we change our example from last week to use the DebuggerStepThroughAttribute, then we can set a break point in the GetFormat method if we suspect it has a defect.
using System;
using System.Diagnostics;
namespace ConsoleApplication1 {
class Program {
static void Main(string[] args) {
PrintMessage(GetFormat(), "Hello World");
}
[DebuggerStepThrough]
static String GetFormat() {
return "MyApp: {0}";
}
static void PrintMessage(String format,
params Object[] args) {
Console.WriteLine(format, args);
}
}
}
For properties, this attribute can only be applied to the get and set blocks, as shown below:
// DebuggerStepThrough cannot be applied here
public Boolean MyProperty {
[DebuggerStepThrough]
get {
return true;
}
[DebuggerStepThrough]
set {
// Set something
}
}
Attributes like DebuggerStepThroughAttribute are only recommendations to the debugger and the debugger is not required to follow them. If you are using Visual Studio, then it’s a safe bet that the debugger will respect them. With that in mind, these attributes can make your debugging life easier.