12

In the c# block comments, I want to say that the default value for a particular parameter is a class const property. Is there a way to reference that parameter directly?

I'd like to either display the value in the generated documentation, or link to the property in some structured way.

This is an example of what I'm trying to do:

public class Foo
{
    private const int DefaultBar = 20;

    ///<summary>
    ///Does the thing.
    ///</summary>
    ///<param name="bar">Description of bar. Defaults to [[DefaultBar]]</param>
    public int DoTheThing(int bar = DefaultBar)
    {
        return bar;
    }
}

Where [[DefaultBar]] above is whatever syntax is necessary to reference the DefaultBar property.

Because it's a constant, I feel like there should be a way to reference it in the generated documentation without manually keeping them in sync. (I don't want to just replace [[DefaultBar]] with 20 in case I want to change 20 later to some other int)

I looked at C# "Constant Objects" to use as default parameters but that question (and associated answers) don't bring up documentation.

Community
  • 1
  • 1
  • Possible duplicate of [C# "Constant Objects" to use as default parameters](http://stackoverflow.com/questions/5372600/c-sharp-constant-objects-to-use-as-default-parameters) – Orel Eraki Jun 27 '16 at 18:13

2 Answers2

11

You can reference a constant like any other code member with the <see> comment tag. In your case it will be:

public class Foo
{
    private const int DefaultBar = 20;

    ///<summary>
    ///Does the thing.
    ///</summary>
    ///<param name="bar">Description of bar. Defaults to <see cref="DefaultBar"/>.</param>
    public int DoTheThing(int bar = DefaultBar)
    {
        return bar;

    }
}

When you generate documentation from these comments, you'll get a link to the constant's page. For example, the output generated with VSdocman (our product) will look like: enter image description here

Peter Macej
  • 4,831
  • 22
  • 49
  • This won't help if I want to integrate that constant as its value inside the comment. For example in texts like "By default the value of x is updated every {{SOME_VALUE}} seconds." – Robert S. Apr 22 '20 at 09:48
3

IntelliSense will automatically keep track of that for you. It would probably be good practice to keep the variable name in the comment like you already do, but when typing out a reference to that method it will be displayed like this:

IntelliSense Example

So whenever you change the value of DefaultBar, IntelliSense will pull the updated value on its own and put it in the tooltip.

Rico
  • 313
  • 2
  • 9