Engage on Facebook Engage on Twitter Engage on LinkedIn Engage on GitHub

Debugger Attributes

By Brian Dukes

When developing code for .NET, there are a number of attributes that you can define on parts of your code to make your job easier while you are debugging.  These are new in .NET 2.0, and part of the System.Diagnostics namespace.

The two attributes here that I have found most helpful are the DebuggerBrowsableAttribute and DebuggerStepThroughAttribute.  I add [DebuggerBrowsable(DebuggerBrowsableState.Never)] as an attribute on most of the private variables in classes I define.  As a result, when debugging, if I look at the class in the Auto or Locals window, it does not show those member as part of the class.  This makes the window much less cluttered when it isn't showing the same data for both the private variables and the public properties.  There are also two other options you can set through DebuggerBrowsable, to display data in a different manner than the default.  However, note that this attribute only works in C# in .NET 2.0. 

DebuggerStepThrough allows you to tell the debugger to automatically step through some section of code, instead of stepping into it each time.  I use this, again, on simple properties, so that I don't have to step through each simple return statement.  It can also be useful in the DotNetNuke world to place it on the Instance() member of your DataProvider, so that you don't have to walk through that method each time you're trying to track down something in the SqlDataProvider.

As an example, here is an email address property from a class that I have designed.  On the private "backing" variable, I set the browsable state to "Never," so that I only see the email address once (from the public property) when I'm debugging the object.  I also set the DebuggerStepThroughAttribute on the get of the public property, so that I don't have to step through the simple return statement each time I'm debugging this object.  Note that you have to set the attributes on the get or set of a property, rather than on the property as a whole.  Since I do some validation in the set, I don't set the DebuggerStepThrough there.

[System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)]
private string _emailAddress;

///


/// Gets or sets the email address of the commenter. Truncates the value if it is longer than
.
///

/// The commenter's email address.


public string
EmailAddress
{
   [System.Diagnostics.DebuggerStepThroughAttribute
()]
   
get
   
{
      return _emailAddress;
   
}
   
set
   
{
      if (String.IsNullOrEmpty(value) || value
.Length <= EmailAddressSizeLimit)
      {
         _emailAddress = value
;
      }
      
else
      
{
         _emailAddress = value
.Substring(0, EmailAddressSizeLimit);
      }
   }
}

There are a number of other debugging attributes in System.Diagnostics.  You can use DebuggerDisplay to define the summary output for a class in the Auto or Locals window.  You can use DebuggerVisualizer to associate a class to act as a visualizer when debugging.  You should try it out, and see how you can make your debugging life simpler just by adding a few attributes to your code.

Check out our free guide, "Six Issues to Avoid When Upgrading to DNN 8" Download