C # 调试: [ DebuggerDisplay ]还是 ToString() ?

有两种方法可以增加调试信息的有用性,而不是在调试器中看到 {MyNamespace.MyProject.MyClass}

这些是 DebuggerDisplayAttributeToString()方法的使用。

using System.Diagnostics;
...


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

或者

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

有什么理由选择其中一个吗?有什么理由不两者兼顾吗?这纯粹是个人偏好吗?

16235 次浏览

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.

"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.

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();

DebuggerDisplay is quite limited in what it can do. You only have a format string that you can use to show values of certain members.

If you want to show data conditionally, data from several levels deep, or aggregated data, ToString() might be your only option.

If you use Xamarin to develop for Android, the ToString method will not be shown in the watch window, but DebuggerDisplay will.

DebuggerDisplay is quicker to type for something simple.

ToString() can be much more complex in case the data you want to view depends on the properties of the object. Combine with #if DEBUG statement and you'll have a good DEBUG-only data viewer. Literally no one uses ToString() as-is for "client view", because it's too vague and any changes would be impossible to maintain logically. Everybody always call explicit properties or methods instead of ToString(), except if providing that string is the sole purpose of the class and it's the same also in debugging.

There also are the DebuggerBrowsableAttribute and DebuggerTypeProxyAttribute, but those are just added complexity if there is no real need for such thing.

Personal Preference (when the desired output is not really 100% known)

  1. Implement HelperFunction
  2. Implement ToString in terms of HelperFunction
  3. Implement DebuggerDisplay in terms of HelperFunction.

Now future edits can either differentiate or remain consistent as appropriate and there is better tracbility.