.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
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.