C# 调试:[DebuggerDisplay] 还是 ToString()?
有两种方法可以提高调试信息的实用性,而不是在调试器中查看 {MyNamespace.MyProject.MyClass}
。
这些是 DebuggerDisplayAttribute
和 ToString()
方法。
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);
}
}
有什么理由更喜欢其中一个而不是另一个?有什么理由不两者兼而有之?这纯粹是个人喜好吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
使用
[DebuggerDisplay]
仅适用于调试器。重写ToString()
具有在运行时更改显示的“副作用”。这可能是好事,也可能不是好事。
通常,您在调试期间需要比标准
ToString()
输出更多的信息,在这种情况下,您将同时使用两者。例如,在您的情况下,
ToString()
实现对我来说似乎很奇怪。我希望Person
类ToString()
实现直接返回Name
,而不是“Name = PersonsName”。但是,在调试过程中,我可能需要额外的信息。Using
[DebuggerDisplay]
is meant only for the debugger. OverridingToString()
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 aPerson
classToString()
implementation to just return theName
directly, not "Name = PersonsName". However, during debugging, I might want that extra information.还可以考虑调试器的缓慢性:
[DebuggerDisplayAttribute()]
格式表达式在每个调试步骤/断点之后由调试器解释。ToString()
在您的代码中编译,因此调试器的执行速度要快得多。这与条件断点相同:如果每次执行到达断点时调试器无法解释条件表达式,则删除断点并添加临时代码可能会很有用,如下所示:
if (condition)调试器.Break();
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();
如果
ToString()
返回并且您在调试器中看到的内容不是您想要的,那么您可以使用DebuggerDisplayAttribute
。If what
ToString()
returns and you see in debugger is not what you would like then you useDebuggerDisplayAttribute
.个人偏好(当所需输出并非真正 100% 已知时)
现在,未来的编辑可以根据情况进行区分或保持一致,并且具有更好的可追踪性。
Personal Preference (when the desired output is not really 100% known)
Now future edits can either differentiate or remain consistent as appropriate and there is better tracbility.
如果您使用 Xamarin 进行 Android 开发,ToString 方法将不会显示在监视窗口中,但 DebuggerDisplay 会显示。
If you use Xamarin to develop for Android, the ToString method will not be shown in the watch window, but DebuggerDisplay will.
DebuggerDisplay
可以更快地输入简单的内容。如果您要查看的数据取决于对象的属性,
ToString()
可能会更加复杂。与#if DEBUG 语句结合使用,您将拥有一个良好的仅调试数据查看器。从字面上看,没有人按原样使用ToString()
来表示“客户端视图”,因为它太模糊了,任何更改都无法在逻辑上维护。每个人总是调用显式属性或方法而不是ToString()
,除非提供该字符串是类的唯一目的,并且在调试中也是如此。还有 DebuggerBrowsableAttribute 和 DebuggerTypeProxyAttribute,但如果没有真正需要这样的事情,这些只会增加复杂性。
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 usesToString()
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 ofToString()
, 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.
DebuggerDisplay
的功能非常有限。您只有一个可用于显示某些成员的值的格式字符串。如果您想有条件地显示数据、多个级别的数据或聚合数据,
ToString()
可能是您唯一的选择。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.