【问题标题】:What is the purpose of defining both <summary> and <value> in a C# automatically implemented property?在 C# 自动实现的属性中同时定义 <summary> 和 <value> 的目的是什么?
【发布时间】:2023-03-19 13:20:01
【问题描述】:

C# 规范/StyleCop(不确定是哪个)建议使用两个标签来注释自动属性——&lt;summary&gt;&lt;value&gt;,给出如下内容:

class Foo
{
    /// <summary>Gets bar.</summary>
    /// <value>Bar.</value>
    public Example Bar { get; set; }
}

但出于所有实际目的,&lt;summary&gt; 的值始终为Gets &lt;whatever you said for value here\&gt;.

这里的单独标签是为了帮助特定的文档生成器,还是与编译器标记自动属性的方式有关,或者其他什么?

【问题讨论】:

  • 如果您开箱即用,那么对于那些使用此属性的人来说,它们是自动实现的这一事实是未知的。只是在这里扮演魔鬼的拥护者......
  • 我认为这只是一个规则。有趣的是,微软提供了一个看起来像这样的例子:&lt;value&gt;The Name property gets/sets the _name data member.&lt;/value&gt;,这对于自动属性来说是没有用的。 Visual Studio 不会自动添加此标记。
  • @Robert:Visual Studio 不会自动添加在一般情况下会通过 StyleCop 的内容。
  • 我不知道那是什么意思。如果您说 Visual Studio 无法确定它是一个属性,也许您是对的。
  • @Robert:不,我的意思是 Visual Studio 的自动文档生成器不关心 C# 规范或 StyleCop。 StyleCop 会因为 Visual Studio 对所有类型的事情感到满意而失败。例如,如果具有公共 getter 和私有 setter 的属性不包含字符串 Gets ,则 StyleCop 将抛出错误。

标签: c# documentation


【解决方案1】:

正如您所指出的,这两个标签的自动建议内容是多余的。 但是,对于一个有据可查的类(例如作为公共 API 的一部分),您在这两个标签中放置的文本应该是不同的。

例如,让我们看一下 Microsoft 针对 DateTime.Date property 的公共 API 文档。

注释中的&lt;summary&gt;&lt;value&gt; 标签对应于文档的两个不同部分。在这种情况下,文档可能是从如下评论生成的:

/// <summary>
///   Gets the date component of this instance.
/// </summary>
/// <value>
///   A new object with the same date as this instance, and the time value
///   set to 12:00:00 midnight (00:00:00).
/// </value>

所以你可以看到,在tooltips中使用的“summary”是属性的缩写摘要,而“value”是对返回值的更详细的描述。

阅读<summary><value> 标签的完整文档了解更多详情。

【讨论】:

  • 啊。所以我放在摘要标签中的内容是错误的。我说的是&lt;summary&gt;Gets a new object with the same date as this instance, and the time value set to 12:00:00 midnight (00:00:00).
猜你喜欢
  • 1970-01-01
  • 2011-10-13
  • 2011-11-26
  • 1970-01-01
  • 1970-01-01
  • 2011-12-28
  • 2012-07-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多