Debugger Attributes

When debugging applications sometimes it’s really nice to be able to tweak things a little. Say you are stepping through segments of your code time and time again and you are tired of stepping into the property getters of your class as you pass a property as a parameter into a method. Or how about when you are using the new debugger visualizers and would love to see some highlights of your custom class without having to click the + sign. In the System.Diagnostic namespace are a few real handy attributes that can make your life easier when debugging.

First is the DebuggerStepThrough Attribute which stops the debugger from stepping into a class, constructor or method (to name a few). If you decorate your property getter when you F11 into a property you will no longer step into your getter code. Below is an example of applying this attribute. 

     public string Address
        {
            //this attribute stops you from going into the address getter
            [DebuggerStepThrough()]
            get { return address; }
            set { address = value; }
        }

    /// <summary>
    /// This class uses the DebuggerStepThrough attribute to stop the debugger
    /// from entering any of the properties getters or setters.
    /// </summary>
    [DebuggerStepThrough()]
    class Customer2
    {
          …code goes here
    }

Next is the DebuggerDisplay Attribute (which is new to .NET 2.0) and is used to display information to the debugger visualizer. The constructor of this attribute accepts a string which is displayed in the value column for instances of the type in either the locals window or the visualizer. The string can contain braces ({ and }) and the text within a pair of braces is evaluated as the name of a field, property, or method. Below is an example of applying this attribute.

     /// <summary>
    /// This class uses the DebuggerDisplay attribute to show highlights of information
    /// stored in the class. This information is displayed in the debugger visualizers
    /// and in the locals window.
    /// </summary>
    [DebuggerDisplay(“FirstName={firstName},LastName={lastName}”)]
    class Customer3
    {
        #region Members
        private string firstName;
        private string lastName;
        private string phone;
        private string address;

        #endregion

        #region Properties
        public string Address
        {
            get { return address; }
            set { address = value; }
        }

        public string Phone
        {
            get { return phone; }
            set { phone = value; }
        }

        public string LastName
        {
            get { return lastName; }
            set { lastName = value; }
        }

        public string FirstName
        {
            get { return firstName; }
            set { firstName = value; }
        }

        #endregion
    }
}

In the editor the attribute changes the default behavior of the visualizer (see below)

 Visualizer1

and in the locals window the attribute changes the value entry (see below).

 Locals

 Finally there is one more attribute that can come in handy when you are using third-party code. The DebuggerNonUserCode Attribute is used to both hide the values from the debugger (similar to the DebuggerHidden Attribute) and stop the debugger from stepping into the code (similar to DebuggerStepThrough Attribute). This attribute can be applied a class, constructor, method or property.

 Here is a sample project that you can use to see how these attributes work.

DebugAttributesDemo.zip

Leave a Reply

Your email address will not be published. Required fields are marked *