Is it possible to have one comment for property and private variable

Hello everyone, my question is very simple.

/// <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; }
}

      

Is there a way to avoid writing the same comment twice? It's just a nuisance.

+3


source to share


5 answers


First, note that you don't need intellisense comments for private members, including fields. Therefore, you can simply delete the first comment. If the value of the field is not obvious from the name, you have not named it appropriately.

Second, for most simple properties, you can remove the explicit field declaration entirely ...



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

      

+6


source


This is not true. But commenting out the property should suffice as the private members are not exposed outside of your class. So just comment on your property.



+5


source


If this is just a direct look at the backing field, just use the auto property to avoid duplication.

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

      

+4


source


Use Auto-Implemented Properties and then you can specify one XML comment for that property.

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

      

But if you are doing something with a private field in your get or set, you need to include the XML comments twice.

One more thing, you probably only need to specify XML comments for the property , since this is public outside of the class and not for your public field.

+2


source


No, there is no way to avoid this as intelisense links wrote a comment on the found code artifact. In your case, you have 2 of them:

  • one is property

    .
  • the other is field

    .

So in your case you will need to write it twice, or as the generated ones suggested, use the auto property and define it once.

+1


source







All Articles