A simple tip to make your debugging sessions go easier

TL;DR: In C# use [DebuggerDisplay("")] on your classes and follow better practices.

So lets say we have a class (C#) that looks like this:

public class Person
{
    public string Name { get; set; }
}

What will Visual Studio’s debugger show me for this guy?

Not very awesome at all. And anything less than awesome, is just no bueno.

We can make this a lot better. All the debugger is doing is calling the ‘ToString’ function on your object which by default is just returning the type name. If you see the {} around the value in the debugger you know that it is calling the ‘ToString’ method. So the first thing we could do is just override the ‘ToString’ method.

public class Person
{
    public string Name { get; set; }
    public override string ToString(){ return Name; }
}

Which gives us:

So we can see that the {} are still there, but now we have the Name property being displayed for our class which can make rapid debugging SO much easier. So this is great and all, but as a web guy I often use the ‘ToString’ function to make my life easier and want to use it for ‘not-debugger’ stuff. So what am I going to do? Suffer? Hell no. Enter the DebuggerDisplayAttribute. Lets change our code to use it.

[DebuggerDisplay("{Name}")]
public class Person
{
    public string Name { get; set; }
}

This gives us:

Bam! Much better. So in the [DebuggerDisplay] attribute all you have to do is follow a convention similar to string.Format. In between {} just put the code you want to evaluate. Yes, you can even use methods. [DebuggerDisplay("{MyMethod()}")] But don’t think that there isn’t a way to shoot your self in the foot with these (Don’t call methods that change state, or you will find yourself in very nasty place).

So, there you go. I simple way to enhance your code for you and your friends. Its a simple thing to add, but doing so can make life so much easier when you are 16 levels deep and trying to keep track of what the hell is going on.

Enjoy.

Better Practices in DebuggerDisplay
MSDN

About Dru Sellers

Sr. Software Engineer at Dovetail Software.
This entry was posted in Uncategorized. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • Anonymous

    have you ever looked at the objectRenders in log4net (not sure if NLog supports the same concept or not)

  • Johnriche123

    Now post your project free on  phpcodelance.com    php freelancers site
    no fee no commission totally free for all and always will free phpcodelance.com

    joomla freelance ,wordpress freelance , seo jobs

    phpjob

  • Don

    nice  

  • Anders Lybecker

    I prefer to
    override the ToString method as I can reuse the method for tracing/logging.

  • Anonymous

    What a nice little trick to add to the bag. :-)

  • RichB

    And when you’re debugging your tests, what do you use? I attach a debugger.

  • Anonymous

    They are both valid approaches. I agree. I use the ToString for a specific thing, which means I can’t use it for debugger. :( But this attribute gives the ability to use both. :) 

  • HackerNewsGuy

    The toString approach seems more helpful to me because it is useful for more than just debuggers…log files, runtime management, etc.  Annotating your source code to make it tailor made for your debugger seems odd to me.

  • Anonymous

    Ah, well we have a different matter of perspective. Do I unit test my code. Sure. Do I do some flavor of TDD, yup. Does everyone? Nope. Do the users of my libraries? Not always. Do I want to be a good citizen in the .Net ecosystem? You betcha! And that is why its worth it, to me, to add this to my classes, it makes someone else’s life easier.

  • http://twitter.com/jediwhale Mike Stockdale

    If you spend so much time in the debugger that it’s worthwhile adding this to all your classes, you might want to look at your development process.  With a robust TDD process, I rarely use the debugger.

  • Anonymous

    such are the dangers of living in the fast lane

  • Anonymous

    see 
    http://blogs.msdn.com/b/jaredpar/archive/2011/03/18/debuggerdisplay-attribute-best-practices.aspx and look at preferred pattern. Also go vote this up for R# to make more awesome.

  • Anonymous

    Thanks!

  • Anonymouscoward

    Someone reading your TLDR too quickly might think you are recommending having string.Empty as the debugger display for every class…

  • http://blog.tcx.be/ Tommy Carlier

    If you don’t want the debugger to put quotes around your display value (so it displays bob instead of “bob”), you can use the nq-modifier, like this:
    [DebuggerDisplay("{Name,nq}")]

  • RichB

    Have you noticed it’s a real PITA when you refactor? And R# gives zero support for it. ho hum