Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 1000
Danko Durbić Avatar asked Dec 17 '20 13:12

Danko Durbić


2 Answers

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.

like image 137
Yair Halberstadt Avatar answered Nov 02 '22 22:11

Yair Halberstadt


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 :

Documentation

like image 39
Gambi Avatar answered Nov 02 '22 22:11

Gambi