简体   繁体   中英

Documentation comments for properties of positional records in C#

If I have a positional record like this:

public record Version(int Major, int Minor);

it seems that I can only provide a <summary> documentation, like this:

/// <summary>
/// Version record
/// </summary>
public record Version(int Major, int Minor);

Is it possible to document the Major and Minor properties (possibly constructor arguments as well) while keeping the short record syntax?

As canton7 points out this is still under development .

The best workaround right now to document the property and the parameters is to define the property explicitly, whilst keeping the record positional:

/// <summary>
/// Version record
/// </summary>
/// <param name="Major">Major constructor parameter</param>
/// <param name="Minor">Minor constructor paramater</param>
public record Version(int Major, int Minor)
{
    /// <summary>
    /// Major Property
    /// </summary>
    public int Major { get; init; } = Major;

    /// <summary>
    /// Minor property
    /// </summary>
    public int Minor { get; init; } = Minor;
}

This is slightly shorter than defining the constructor yourself, but more usefully, once support is added you can move the documentation to the primary constructor whilst knowing that this won't change the generated code in any way. Also it is necessary to do it this way if you want to inherit from another positional record.

You can document it like this:

/// <summary>
/// Version record
/// </summary>
/// <param name="Major">Major version</param>
/// <param name="Minor">Minor version</param>
public record Version(int Major, int Minor);

And then you have the documentation while using it in-code:

文档

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM