是否可以对属性和私有变量进行一次注释

时间:2013-02-07 11:49:34

标签: c# properties comments

大家好,我的问题很简单。

/// <summary>
/// a description here. for internal use
/// </summary>
private A _Owner;
/// <summary>
/// Same description also here. for outside use
/// </summary>
public A Owner
{
    get { return _Owner; }
    set { _Owner = value; }
}

有没有办法避免两次写同一个评论?这只是一件令人讨厌的事。

5 个答案:

答案 0 :(得分:6)

首先,请注意您不需要智能成员的智能感知评论,包括字段。所以你可以删除第一条评论。如果字段的含义在名称中不明显,那么您没有对其进行适当的命名。

其次,对于大多数简单属性,您可以完全删除显式字段声明...

/// <summary>a description here</summary>
public A Owner {get;set;}

答案 1 :(得分:5)

不是。但是评论该属性应该足够了,因为私人成员不会出现在你的课堂之外。所以只需评论你的财产。

答案 2 :(得分:4)

如果它只是直接查看支持字段,只需使用自动属性即可避免重复。

/// <summary>
/// just use an autoprop
/// </summary>
public A Owner
{
    get;set;
}

答案 3 :(得分:2)

使用 Auto implemented properties ,然后您可以为该属性指定单个XML注释。

/// <summary>
/// Same description also here. for outside use
/// </summary>
public A Owner
{
get; set;
}

但是如果你在get或set中使用私有字段做某事,那么你必须指定两次XML注释。

还有一件事,您可能只需要为属性指定XML注释,因为它在类外部公开,而不是公共字段。

答案 4 :(得分:1)

不,没有办法避免这种情况,因为intelisense会将链接写入注释到找到的代码工件。在您的情况下,您有 2

  • 一个是property
  • 另一个是field

所以在你的情况下,你需要写两次,或者,如同施法者建议的那样,使用auto属性并定义一次。

相关问题