Documentation comments for record types with primary constructors in C#
Asked Answered
H

0

6

I can declare a C# positional record like this:

public record Box(double Capacity);

which results in the definition of a record class named Box with a property named Capacity, as well as a primary constructor for Box that takes a parameter named Capacity. Now I want to be able to create independent documentation comments for all four of these items:

  1. The record type Box.
  2. The property Capacity.
  3. The primary constructor.
  4. The constructor parameter Capacity.

If I create a documentation comment like this:

/// <summary>
/// A container for items.
/// </summary>
/// <param name="Capacity">How much the Box can hold</param>
public record Box(double Capacity);

then I get the same summary comment for both the Box type and the constructor, and the same parameter comment for both the Box property and the constructor parameter.

As discussed in this question, I can create independent comments for the property and the constructor parameter by explicitly declaring the property:

/// <summary>
/// A container for items.
/// </summary>
/// <param name="Capacity">Specifies the size of the box</param>
public record Box(double Capacity) {

    /// <summary>
    /// Returns the size of the box.
    /// </summary>
    public double Capacity { get; init; } = Capacity;
}

And I can create independent comments for all of the parts by explicitly declaring the constructor as well:

/// <summary>
/// A container for items.
/// </summary>
public record Box {

    /// <summary>
    /// Returns the size of the box.
    /// </summary>
    public double Capacity { get; init; }

    /// <summary>
    /// Creates a new Box instance.
    /// </summary>
    /// <param name="Capacity">Specifies the size of the box</param>
    public Box(double Capacity) {
        this.Capacity = Capacity;
    }
}

But now I have completely lost the syntactic elegance of the record type! Is there any way to retain the concise record declaration with positional parameters while generating independent documentation comments for all of its generated parts?

Hulahula answered 31/3 at 13:56 Comment(3)
Interesting question, and well laid out but perhaps not really one we're well equipped to answer as it's essentially "what was the .net design team thinking when..". You can, however, ask them - github.com/dotnet/core/issuesConcoction
My take on it (and it's opinion, hence not an answer) is that records serve a distinct and small purpose, to concisely represent a reference type that will have limited use in one area of my app, such as being a viewmodel only in particular razor page X.. as such I'm not expecting to have to/need to document them chapter and verse for an external user to know all their nuances; there won't be an external user and I'd mostly use them in similar ways to how I would use an anonymous type - obvious in its context only. A fully expanded documented class would be used elsewhereConcoction
I fear that I may have misled some readers with my use of the term "elegance" in the background to the question. For clarification, my specific question is contained in this one sentence at the end of the original post: "Is there any way to retain the concise record declaration with positional parameters while generating independent documentation comments for all of its generated parts?". I believe this is a purely fact-based question about the syntax of the language and structured comments, and I'm sorry if the background information caused any confusion.Hulahula

© 2022 - 2024 — McMap. All rights reserved.