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

发布于 2024-09-08 07:00:42 字数 666 浏览 5 评论 0原文

有两种方法可以提高调试信息的实用性,而不是在调试器中查看 {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);
    }
}

有什么理由更喜欢其中一个而不是另一个?有什么理由不两者兼而有之?这纯粹是个人喜好吗?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

请恋爱 2024-09-15 07:00:42

使用[DebuggerDisplay]仅适用于调试器。重写 ToString() 具有在运行时更改显示的“副作用”。

这可能是好事,也可能不是好事。

通常,您在调试期间需要比标准 ToString() 输出更多的信息,在这种情况下,您将同时使用两者。

例如,在您的情况下, ToString() 实现对我来说似乎很奇怪。我希望 PersonToString() 实现直接返回 Name,而不是“Name = PersonsName”。但是,在调试过程中,我可能需要额外的信息。

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.

套路撩心 2024-09-15 07:00:42

还可以考虑调试器的缓慢性:

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

去了角落 2024-09-15 07:00:42

“当您创建自定义类或结构时,您应该覆盖
ToString 方法,以便提供有关您的类型的信息
客户端代码。”
MSDN

如果 ToString() 返回并且您在调试器中看到的内容不是您想要的,那么您可以使用 DebuggerDisplayAttribute

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

ゃ懵逼小萝莉 2024-09-15 07:00:42

个人偏好(当所需输出并非真正 100% 已知时)

  1. 实现 HelperFunction
  2. 根据 HelperFunction 实现 ToString
  3. 根据 HelperFunction 实现 DebuggerDisplay。

现在,未来的编辑可以根据情况进行区分或保持一致,并且具有更好的可追踪性。

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.

梦初启 2024-09-15 07:00:42

如果您使用 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.

清浅ˋ旧时光 2024-09-15 07:00:42

DebuggerDisplay 可以更快地输入简单的内容。

如果您要查看的数据取决于对象的属性,ToString() 可能会更加复杂。与#if DEBUG 语句结合使用,您将拥有一个良好的仅调试数据查看器。从字面上看,没有人按原样使用 ToString() 来表示“客户端视图”,因为它太模糊了,任何更改都无法在逻辑上维护。每个人总是调用显式属性或方法而不是 ToString(),除非提供该字符串是类的唯一目的,并且在调试中也是如此。

还有 DebuggerBrowsableAttributeDebuggerTypeProxyAttribute,但如果没有真正需要这样的事情,这些只会增加复杂性。

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.

以可爱出名 2024-09-15 07:00:42

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文