65.9K
CodeProject is changing. Read more.
Home

Use DebuggerBrowsable Attribute for a better view in debugger window

emptyStarIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

0/5 (0 vote)

Feb 4, 2010

CPOL
viewsIcon

8160

There are lots of simple tricks in .net which can be very useful if you used wisely. In this post I want to discuss the use of DebuggerBrowsableAttribute. Using this attribute you can control how a member of class will be displayed in debugger windows during debugging. Before that consider small...

There are lots of simple tricks in .net which can be very useful if you used wisely. In this post I want to discuss the use of DebuggerBrowsableAttribute. Using this attribute you can control how a member of class will be displayed in debugger windows during debugging. Before that consider small example:
public class Customer
    {

        public string CustomerId
        {
            set;
            get;
        }
        public string CustomerName
        {
            set;
            get;
        }
        public string CustomerLocation
        {
            set;
            get;
        }
        public string CustomerGuid
        {
            set;
            get;
        }
    }
Lets create a instance of Organization and add some sample data:
List<Customer> customers = new List<Customer>();
customers.Add(new Customer { CustomerId = "1", CustomerName = "BenCustomer", CustomerLocation = "New York", CustomerGuid = System.Guid.NewGuid().ToString() });
customers.Add(new Customer { CustomerId = "2", CustomerName = "JonhCustomer", CustomerLocation = "London", CustomerGuid = System.Guid.NewGuid().ToString() });
customers.Add(new Customer { CustomerId = "3", CustomerName = "HarryCustomer", CustomerLocation = "Sydney", CustomerGuid = System.Guid.NewGuid().ToString() });
customers.Add(new Customer { CustomerId = "4", CustomerName = "CrisCustomer", CustomerLocation = "Kolkata", CustomerGuid = System.Guid.NewGuid().ToString() });
Now when you run this program in debug mode and try to view the customers in debugger window . Lets try to change it using DebuggerBrowsableAttribute. Make following changes by adding DebuggerBrowsableAttribute:
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
public string CustomerGuid
{
   set;
   get;
}
Again run this program in debug mode and try to view the customers in debugger window. Now You can see how “CustomerGuid” is gone out.