Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# debugging: [DebuggerDisplay] or ToString()?

There are two ways to increase the usefulness of debugging information instead of seeing {MyNamespace.MyProject.MyClass} in the debugger.

These are the use of DebuggerDisplayAttribute and the ToString() method.

using System.Diagnostics;
...

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

or

public class Person
{
    public string Name;
    public override string ToString()
    {
        return string.Format("Name = {0}", Name);
    }
}

Is there any reason to prefer one to the other? Any reason not to do both? Is it purely personal preference?

like image 888
bwerks Avatar asked Jul 06 '10 23:07

bwerks


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


3 Answers

Using [DebuggerDisplay] is meant only for the debugger. Overriding ToString() has the "side effect" of changing the display at runtime.

This may or may not be a good thing.

Often, you want more info during debugging than your standard ToString() output, in which case you'd use both.

For example, in your case, the "ToString" implementation seems odd to me. I would expect a "Person" class ToString() implementation to just return the Name directly, not "Name = PersonsName". However, during debugging, I might want that extra information.

like image 93
Reed Copsey Avatar answered Oct 16 '22 12:10

Reed Copsey


Slowness of the debugger can also be taken into account:

DebuggerDisplayAttribute format expression is interpreted by the debugger after each debugging step / breakpoint.

ToString is compiled in your code and is therefore much faster to execute by the debugger.

That's the same with conditional breakpoints: If the conditional expression is too slow to interpret by the debugger each time the execution reach the breakpoint, it can be useful to remove the breakpoint and instead add temporary code like this: if (condition) Debugger.Break();

like image 7
Wizou Avatar answered Oct 16 '22 14:10

Wizou


"When you create a custom class or struct, you should override the ToString method in order to provide information about your type to client code." — MSDN

If what ToString() returns and you see in debugger is not what you would like then you use DebuggerDisplayAttribute.

like image 7
Piotr Perak Avatar answered Oct 16 '22 12:10

Piotr Perak