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!

1 Comment

Comments have been disabled for this content.