.NET Tip of The Day
Learn one new .NET trick every day
Быстрое пополнение счета телефона      Login or Join

Use DebuggerBrowsable attribute to clean up class view in a debugger

The DebuggerBrowsable attribute determines if and how a field or property is displayed in the debugger variable windows. Let's review the following code example:

    public class VehicleSeries

    {

        private string _name;

        public string Name

        {

            get { return _name; }

            set { _name = value; }

        }

 

        private List<string> _vehicles = new List<string>();

        public List<string> Vehicles

        {

            get { return _vehicles; }

            set { _vehicles = value; }

        }

    }

The resulting display within the debugger is shown below:

    DataTip 1

Now lets remove duplicate information and show constituent vehicles by adding a few DebuggerBrowsable attributes:

    public class VehicleSeries

    {

        [DebuggerBrowsable(DebuggerBrowsableState.Never)]

        private string _name;

        public string Name

        {

            get { return _name; }

            set { _name = value; }

        }

 

        [DebuggerBrowsable(DebuggerBrowsableState.Never)]

        private List<string> _vehicles = new List<string>();

        [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]

        public List<string> Vehicles

        {

            get { return _vehicles; }

            set { _vehicles = value; }

        }

    }

This results in the following view in the debugger:

    DataTip 2

P.S. Also take a look at a tip "Use DebuggerDisplay attribute for a better debugger experience".

12/2/2007

Comments:

Add the DebuggerNonUserCode attribute above the 'get' and the 'set', and forget about the field. The debugger won't even step into the property code.
Don't forget to turn on the 'Just my code' feature in the VS Debugger settings.

Adrian Aisemberg 12/5/2007 4:10:22 PM

IMHO the DebuggerStepThrough attribute serves better for this purpose - http://dotnettipoftheday.org/tips/DebuggerStepThroughAttribute.aspx

kostya.ly 12/6/2007 1:36:14 PM

When using the DebuggerNonUserCode attribute, you can still step into the code if you uncheck the 'Just my code' option.
If you use the DebuggerStepThrough attribute, you cannot step into it, but only if you stop, edit and compile again without that attribute.
So the difference is in the control.
Means, you can choose at debug-time, if you still want to step into a 'hidden' code.

Adrian Aisemberg 12/6/2007 1:44:21 PM

In contrast to DebuggerNonUserCode, DebuggerStepThrough allows a breakpoint to be set in a method. Therefore if you want to step into the method marked with DebuggerStepThrough attribute, just set a breakpoint in it.

kostya.ly 12/6/2007 4:07:27 PM

Suppose you have a dll reference (not a project-reference), and still, you have the code of that dll (a real-world example).
When you want to step into a method of that dll, if the method is marked with DebuggerStepThrough, you'll have to find that code and put the breakpoint in it. 'Go To Definition' won't help in this case.
If the method is marked with DebuggerNonUserCode, just toggle the 'Just my code' feature (no need to stop the debugger for that) and step into it. The code will be loaded automatically without the need to locate it manually.

Adrian Aisemberg 12/21/2007 12:59:01 AM

Cool feature.. keep it posting such TIPS.

Nilesh 4/6/2010 3:21:37 PM

Name
URL
E-mail
Provide your e-mail address to receive notification about new comments.
Message
HTML tags are not supported.
Please add 8 and 2 and type the answer here:
RSS .NET Tip of The Day
Subscribe to receive one tip from the .NET Tips and Tricks Community per day.
Previous Tips of The Day
The best of the .NET Tips & Tricks Community.
.NET Practitioners .NET Tips & Tricks Community
Every .NET practitioner has a trick up in their sleeve. This is the place to share it with other .NET people.
Submit a Tip
Discovered a new trick? Share it with others.
My Tips
Manage tips you authored.