Click here to Skip to main content
15,886,720 members
Articles / Programming Languages / C#
Tip/Trick

Use DebuggerBrowsable Attribute for a better view in debugger window

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
4 Feb 2010CPOL 7.9K  
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:

C#
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:

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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Kovair Inc.
India India
I have started programming in 10 years on Fortran 77.Then it was Cobol,C,C++,JAVA 1.2, ASP,JavaScript. Now it at all C# and .NET Framework, such tehnologies as WCF, ADO.NET, ASP.NET, LINQ, ASP.NET Ajax , FLEX and another.

Comments and Discussions

 
-- There are no messages in this forum --