Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to reference const parameters in C# block comments?

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.

like image 389
Chris Reyes - he-him Avatar asked Jun 27 '16 18:06

Chris Reyes - he-him


People also ask

Can parameters be passed by reference in C?

Passing by by reference refers to a method of passing the address of an argument in the calling function to a corresponding parameter in the called function. In C, the corresponding parameter in the called function must be declared as a pointer type.

How do you reference a constant?

A constant reference is an expression that evaluates to the value of the named constant. The simplest constant references are primary expressions—they consist simply of the name of the constant: CM_PER_INCH = 2.54 # Define a constant. CM_PER_INCH # Refer to the constant.

Can you have a constant reference?

The grammar doesn't allow you to declare a “const reference” because a reference is inherently const . Once you bind a reference to refer to an object, you cannot bind it to refer to a different object.


1 Answers

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

like image 171
Peter Macej Avatar answered Sep 19 '22 12:09

Peter Macej