This is a mirror of official site: http://jasper-net.blogspot.com/

A simple tip to make your debugging sessions go easier

| Sunday, November 20, 2011
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?

weak_sauce_display.png

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:

hot_sauce.png

Read more: CodeBetter
QR: https://chart.googleapis.com/chart?chs=80x80&cht=qr&choe=UTF-8&chl=codebetter.com/drusellers/2011/11/17/a-simple-tip-to-make-your-debugging-sessions-go-easier/

Posted via email from Jasper-net

0 comments: