Good ol’ DebuggerDisplay attribute

I got to know about this DebuggerDisplay attribute that you can use on your types for ease of debugging, seems like this has been there since VS2005.

Lets’ get started. I have an Employee class which is being used somewhere in the code. Just as any other developer, I’m want to know the values on the properties of the instance of this class.

   1: // callee
   2: DebuggerDisplayOnEmployee(new Employee {EmployeeId = 1, 
   3:                                         FirstName = "Arun", 
   4:                                         LastName = "Mahendrakar"});
   5:  
   6: private static void DebuggerDisplayOnEmployee(Employee employee)
   7: {
   8:     // use employee instance here
   9: }

When I’m debugging this, this is how VS shows me the instance.

image

I’ll have to collapse the symbol to see what values are set to the properties. Let’s see what happens when we override the ToString() method.

   1: public override string ToString()
   2: {
   3:     return string.Format("{0}-{1}-{2}", EmployeeId, FirstName, LastName);
   4: }

Now it’s more direct to see what values the properties have right on the first line.

image

But what if I don’t want or, more realistically, cannot override the ToString() method in all cases? DebuggerDisplay attribute comes to the rescue.

   1: [System.Diagnostics.DebuggerDisplay("EmployeeId={EmployeeId},FirstName={FirstName},LastName={LastName}")]
   2: public class Employee
   3: {
   4:     public int EmployeeId { get; set; }
   5:     public string FirstName { get; set; }
   6:     public string LastName { get; set; }
   7:  
   8: }

You add a string value with whatever you want to be displayed while debugging. Note that I’ve removed the overridden ToString() method from the class for this purpose.

image

Whoopytah, much better.

But pushing it a little further, what happens if I have both – the DebuggerDisplay attribute defined and the ToString() method overridden?

   1: [System.Diagnostics.DebuggerDisplay("EmployeeId={EmployeeId},FirstName={FirstName},LastName={LastName}")]
   2: public class Employee
   3: {
   4:     public int EmployeeId { get; set; }
   5:     public string FirstName { get; set; }
   6:     public string LastName { get; set; }
   7:  
   8:     public override string ToString()
   9:     {
  10:         return string.Format("{0}-{1}-{2}", EmployeeId, FirstName, LastName);
  11:     }
  12: }

Turns out DebuggerDisplay attribute wins in such a case.

image

The DebuggerDisplay attribute works on complex types as well.

   1: [DebuggerDisplay("{FirstName} {LastName} is working on {AssignedToProject.SponsoringCompany.Name}'s project")]
   2: public class Employee
   3: {
   4:     public int EmployeeId { get; set; }
   5:     public string FirstName { get; set; }
   6:     public string LastName { get; set; }
   7:  
   8:     public Project AssignedToProject { get; set; }
   9: }
  10:  
  11: public class Project
  12: {
  13:     public string Name { get; set; }
  14:     public Company SponsoringCompany { get; set; }
  15: }
  16:  
  17: public class Company
  18: {
  19:     public string Name { get; set; }
  20: }

You just have to make sure you give the names of the properties all the way through and you’ll get the output of:

image

Just to be in the clear, you can accomplish all this by overriding the ToString() methods as well, but as said above, you might not be in a position to override the ToString() method for all the types.

Now, the two scenarios where IMHO this attribute will be best used:

  • when you have a complex type as above and you don’t want to drill-down 7 or so times to see the value on the property and
  • when you have a type which has quite a few properties where you’ll have to scroll down to check what value has been set on a property (especially the property name that starts with say, ‘W’)

If you think of any other scenarios, please leave a comment and I’ll update the list.

Disclaimer: The names used are not representative of actual values and any resemblance is purely co-incidental!

Published Wednesday, September 28, 2011 10:16 PM by nmarun
Filed under: , ,

Comments

# re: Good ol’ DebuggerDisplay attribute

Wednesday, September 28, 2011 8:54 PM by David

You going to show us how to write a DebuggerVisualizer now?

Thanks.

# Good ol??? DebuggerDisplay attribute | .NET, ASP.NET, C# | Syngu

Pingback from  Good ol??? DebuggerDisplay attribute | .NET, ASP.NET, C# | Syngu

# Good ol??? DebuggerDisplay attribute

Thursday, September 29, 2011 3:20 AM by Good ol??? DebuggerDisplay attribute

Pingback from  Good ol??? DebuggerDisplay attribute

# re: Good ol’ DebuggerDisplay attribute

Thursday, September 29, 2011 8:13 AM by David

This is a good article and offer some helpful information for me,thank you!

# re: Good ol’ DebuggerDisplay attribute

Monday, May 14, 2012 7:26 PM by Bhumika

I agree left / right joins are very important but sedlom used. Not relevant in some situation but when they are called for they do a much better job than inner joins. I guess people simply don't know that they exist, and how they work.Nice tutorial.

Leave a Comment

(required) 
(required) 
(optional)
(required)