Hi

 

I wanted to show the usage of these two very useful debugger attributes. If you don’t know them keep on reading, they are very useful.

 

DebuggerDisplay

This attribute allows you to customize the way an object is displayed. lets look at the following example:

   1: class Program
   2: {
   3:     static void Main(string[] args)
   4:     {
   5:         Client c = new Client() 
   6:         { 
   7:             Name = "John", 
   8:             HomeNumber = "555-0094", 
   9:             AccountBalance = -5000 
  10:         };
  11:     }
  12: }
  13:  
  14: class Client
  15: {
  16:     public string Name { get; set; }
  17:     public string HomeNumber { get; set; }
  18:     public double AccountBalance { get; set; }
  19: }

When we hover above the object c we get the following:

No Debugger Attribute

We can make this much more “appealing” by using the DebuggerDisplay attribute. By using the DebuggerDisplay you can create a string that will replace what Visual Studio shows by default. For example you can add the following attribute:

   1: [DebuggerDisplay(" This is a client Named {Name}, His Home Phone Number" +
   2:         "is {HomeNumber} and his Account Balance is {AccountBalance}")]

Now once we hover above the object c we get the following:

DebuggerDisplay Attribute

Pretty neat no?

You can customize the view in almost any way you want and you can even call method that belong to the class inside the marked {}.

 

DebuggerBrowsable

Lets add to the client class a list of strings that will represent SubAccounts he might have. Using DebuggerBrowsable we have 3 options to mark that list and one of them is extremely useful:

  1. DebuggerBrowsableState.Collapsed: This is the default value and keeps the view exactly the way you know.

    DebbugerBrowsable.Collapsed

  2. DebuggerBrowsableState.RootHidden: This makes the root of the List hidden and expands the list, so a 3 items list will look like this:

    DebuggerBrowsable.RootHidden

    As you can see the list is all expanded and the List Data is under the Raw View. If you ask me I don’t really see a real life use for this.

  3. DebuggerBrowsableState.Never: This makes the list go away! you cant see it in debug. I find this useful for people who write infrastructures in their company. Sometimes you don’t want to other users to see that list. This is your way of hiding it:

    DebuggerBrowsable.Never 
    How about that, No List!

More on other Debugger Attributes soon

 

Enjoy.

 

Amit

Tags :

2 Responses to “DebuggerDisplay and DebuggerBrowsable – Two Debugger Attributes you should know”


  1. Gerardo Contijoch

    Said on April 10, 2009 :

    Nice post. I would like to add it is possible to apply DebuggerDisplayAttribute at assembly level too in case you don’t want or can’t apply the attribute directly on the clases.

    Other useful feature is the simple expression evaluation inside {}. It is possible to write stuff like this:

    [DebuggerDisplay(“This is a client Named {Name}, His Home Phone Number is {HomeNumber} and his financial status is {(AccountBalance – Debts) > 0 ? \”Good\” : \”Bad\”}”)]

    But this is only in C#.

    For those who read spanish, last month I posted two articles about DebuggerDisplayAttribute and DebuggerTypeProxyAttribute in my blog.

  2. Caleb

    Said on September 23, 2010 :

    This is good to know. Thanks for sharing.

    Note that you can override the ToString method to get a custom description in the debugger visualizer as well. Overriding ToString has the added advantage of being able to be reused in your code easily.

Post a Comment