Tom Goff's .Net Musings

Tidbits of information regarding .Net, C#, and SQL Server.

COTW: DebuggerStepThroughAttribute Class

leave a comment »

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.

Advertisements

Written by Tom

November 5, 2007 at 7:32 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: